Reverse Order of String Like "Hello Word" Reverse as "Word Hello" in PHP

get every words backwards using php

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

Reverse the order of space-separated words in a string

Try this:

$s = 'Hello everybody in stackoverflow';
echo implode(' ', array_reverse(explode(' ', $s)));

Reverse the order of letters in the last word in a string

I have been Found the answer. For my own question

$split = explode(" ", $words);
$v_last = $split[count($split)-1];
$reverse = strrev($v_last);
$f_word = chop($words,$v_last);
echo $f_word;
echo $reverse;

Reverse the direction of all sequences of letters in a string

 $word = "word,!pineapple--pizza";
$revd = preg_replace_callback('#([A-Za-z]+)#', 'rev_first', $word);

function rev_first($matches){
return strrev($matches[1]);
}

http://3v4l.org/PXEs8

Is this what you're looking for?

Reverse letters in each word of a string without using native splitting or reversing functions

$string = "I am a boy";

$reversed = "";
$tmp = "";
for($i = 0; $i < strlen($string); $i++) {
if($string[$i] == " ") {
$reversed .= $tmp . " ";
$tmp = "";
continue;
}
$tmp = $string[$i] . $tmp;
}
$reversed .= $tmp;

print $reversed . PHP_EOL;
>> I ma a yob

Reverse a string with php

As others said, there's strrev() to do this.

If you want to build it on your own (for learning?): your problem is that you're starting with your index one too high - a string of length 25 is indexed from 0 to 24, so your loop has to look like this:

for ($i = $len - 1; $i >=0;$i--)
{
echo $stringExp[$i];
}

Reverse each word of a string separately

$Str   = 'Hello world';
$Words = preg_split('/\s+/', $Str); // or explode(' ', $Str);

foreach ($Words as $Word)
echo strrev($Word) . ' ';

output

olleH dlrow 


Related Topics



Leave a reply



Submit