i need to reverse String "Hello Word" into "Word Hello", it could be "My Name is Khan" reverse into "Khan is Name My" in PHP
-
1We're not going to simply do your work/homework for you. You'll have to show that you have at least put some research effort into this issue. Once you have tried something, come update this post with some code and a specific problem you are facing. – Lix Oct 03 '13 at 09:01
-
What you have posted here is just a list of requirements. – Lix Oct 03 '13 at 09:02
-
`$string = 'Hello World'; $sArray = explode(' ', $string); $sArray = array_reverse($sArray); $string = implode(' ',$sArray); echo $string;` - there's your PhD Thesis done for you – Mark Baker Oct 03 '13 at 09:03
-
Related: [Reverse the ordering of words in a string](https://stackoverflow.com/q/1009160/2943403) – mickmackusa Dec 29 '21 at 04:07
-
Dupe: https://stackoverflow.com/questions/715330/reverse-string-php – mickmackusa Dec 29 '21 at 06:05
4 Answers
$str = "My Name is Khan";
$reverse = implode(" ",array_reverse(explode(" ", $str)));
echo $reverse;
Result is Khan is Name My
.
explode
splits the string into an array in accordance to the delimiter, which in this case is " "
. array_reverse
is self-explanatory, it reverses the order of the array. implode
then joins the string using the delimiter.

- 31,623
- 10
- 79
- 80
You might think to code something to show then let us help you with the answer, but let me describe the logic, since you are going to input it as a string, you might think to split the character based on the character you wanted to split
you might consider this EXPLODE
After you finish and put that split into an array. You can reverse it back using looping or perhaps you want to reverse using THIS

- 57,590
- 26
- 140
- 166
-
This is the only acceptable answer here as it actually tried to teach the OP something as opposed to just giving the answer. – Lix Oct 03 '13 at 09:06
At first, requirement is: reverse the order of words not alphabets.
So, need to split the string by spaces(as words are separated by spaces).
This way, an array is generated like:
array('World', 'Hello');
Change the sequence of array using array_reverse()
The resulting array would be:
array('Hello', 'World');
Now, again join the above array by space:
The resulting string would be:
Hello World
Try this:
<?php
$name = 'World Hello';
$temp = explode(' ', $name);
echo '<pre>';
print_r($temp);
echo '</pre>';
$arr = array_reverse($temp);
echo '<pre>';
print_r($arr);
echo '</pre>';
$str = implode(' ', $arr);
echo $str;
?>

- 23,834
- 6
- 44
- 66
-
3Fantastic... just copy paste and nothing is learnt. Please don't encourage these zero effort questions by providing an answer... – Lix Oct 03 '13 at 09:05
-
Old pages are used to close new pages. When old pages do not include explanations, the new asker may not understand how/why the snippet works. Please always include an explanation with every answer. – mickmackusa Dec 28 '21 at 09:12
-
$string = "My Name is Khan";
$new = explode(' ', $string);
$new = array_reverse($new);
print_r(implode(' ', $new));

- 5,911
- 4
- 31
- 58
-
1Fantastic... just copy paste and nothing is learnt. Please don't encourage these zero effort questions by providing an answer... – Lix Oct 03 '13 at 09:05