get every words backwards using php [duplicate] - php

This question already has answers here:
Reverse Order of String like "Hello Word" reverse as "Word Hello" in PHP [closed]
(4 answers)
Closed 1 year ago.
I want to get the text and print every words backwards.
Example: Hello World
Output: World hello
This is my codes so far. It is different, I get output backwards but per string.
$string = "hello world";
$length = strlen($string);
for ($i=($length-1) ; $i >= 0 ; $i--)
{
echo $string[$i];
}
Output of the above code is:
dlrow olleh

Another way to do it with array_reverse(),
<?php
$str = 'Hello World';
$strArray = explode(" ", $str);
$strArray = array_reverse($strArray);
$str = implode($strArray, " ");
echo $str;
?>
DEMO: https://3v4l.org/ZfEqQ

Related

How do I remove string after or before space in PHP? [duplicate]

This question already has answers here:
Return the portion of a string before the first occurrence of a character in PHP [duplicate]
(6 answers)
Closed last month.
How do I remove string after or before space in PHP?
I have a string like $string = "Hello World";
The output should be "Hello"
How can I do that?
I have a string like $string = "Hello World";
The output should be "Hello"
Well, you can explode on <space> and take the first element of the resulting array:
<?php
$a = "Hello World";
$arr = explode(" ",$a);
echo $arr[0]; // Hello
I can't help but think there's more to it than this.

How to find the most used word in a string PHP [duplicate]

This question already has answers here:
Most used words in text with php
(4 answers)
Closed 5 years ago.
I'm trying to find the most used word in a string in PHP. The lyrics file is a string of lyrics.
//display the most used word in the lyrics file
$wordCount = str_word_count($lyrics);
$wordCountArray = array();
foreach($wordCount as $word){
if(!array_key_exists($word, $wordCountArray)){
$wordCountArray[$word] = 1;
}else{
$wordCountArray[$word] += 1;
}
}
arsort($wordCountArray);
print_r($wordCountArray[0]);
I'm getting errors with this code and I'm wondering what isn't working. I need the actual word and not the number.
I think you meant:
$words = str_word_count($lyrics, 1)
foreach($words as $word) {
Simple example
<?php
$string = 'Hello hi hello abcd abc hoi bye hello';
$string = strtolower($string);
$words = explode(' ',$string);
var_dump(array_count_values($words));

extracting numbers from string in php [duplicate]

This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 7 years ago.
I'm wondering how to extract numbers from a string for example
$string = "1.8 to 250"
What i want to get is,
$a = 1.8
$b = 250
Thanks for any help provided
Try this :
$str = '1.8 to 250';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
From this post Extract numbers from a string
Try this:
$str = '1.8 to 250';
$string = explode(" ",$str);
//print_r($string);
for($i = 0;$i < count($string);$i++){
if(is_numeric($string[$i])){
print "\n $string[$i]";
}
}

Remove all zero values from string PHP [duplicate]

This question already has answers here:
How to strip trailing zeros in PHP
(15 answers)
Closed 7 years ago.
I have a string like this:
14522354265300000000000
I want to display it without zero values, how I can do this? I do this
$pos = strpos($route, '0');
$length = count(str_split($route));
$a = $length - $pos;
$a = substr($route, 0, $a);
but it remove 3 in the end of string. Can somebody help me?
Additional:
If string will be 123088888880, I want make it 123.
You can use rtrim for this:
echo rtrim("14522354265300000000000", "0"); // outputs: 145223542653
here's a nice algo:
<?php
$string = "14522354265300000000000";
$new_string = '';
for($i=0; $i<strlen($string) ; $i++){
if($string[$i] != '0'){
$new_string .= $string[$i];
}
}
echo $new_string;
?>
rtrim is only if you have zero's at end of string :)
You can use rtrim('14522354265300000000000', '0')

Make string shorter. Cut it by the last word [duplicate]

This question already has answers here:
Get first 100 characters from string, respecting full words
(18 answers)
Closed 8 years ago.
How to make string shorter by cut it on the last word?
like example, allowed symbols are 10, and echo only these words which fits in this limit.
$string = 'Hello Hello John Doe'
// Limit 10. Expected result:
$string = 'Hello'
// Limit 12. Expected result:
$string = 'Hello Hello'
...
All I can find in manual is cutting string by symbols, not by words. There are some custom functions to do so, but maybe there are php command for this?
This should work:
$str = "i have google too";
$strarr = explode(" ", $str);
$res = "";
foreach($strarr as $k)
{
if (strlen($res.$k)<10)
{
$res .= $k." ";
}
else
{
break;
};
}
echo $res;
http://codepad.org/NP9t4IRi
Tried to edit Mike's answer, to fix the last word thing, but was not able to.
So here is his solution with the fix:
$str = "Hello Hello My name is Hal";
$len = 10;
if ( strlen( $str ) > $len )
{
$out = substr($str,0,$len);
if ( $str[$len] != ' ')
{
$out = substr($out,0,strrpos($out,' '));
}
}
echo $out; // Hello
Edit: update version to cope with word breaks better.
This shouldn't be too difficult. Truncate to the maximum length, then truncate to the last space. Add an adjustment for lengths that fall on the end of words
<?php
$str = "Hello Hello My name is Hal";
for ($i = 3; $i <30;$i++) {
echo "'".trunc($str,$i)."'\n";
}
function trunc($str, $len) {
$str.=' ';
$out = substr($str,0,$len+1);
$out = substr($out,0,strrpos($out,' '));
return trim($out);
}
Here's a codepad version

Categories