What would be the best way to reverse the order of a string so for instance,
'Hello everybody in stackoverflow'
becomes
'stackoverflow in everybody Hello'
any ideas
What would be the best way to reverse the order of a string so for instance,
'Hello everybody in stackoverflow'
becomes
'stackoverflow in everybody Hello'
any ideas
Try this:
$s = 'Hello everybody in stackoverflow';
echo implode(' ', array_reverse(explode(' ', $s)));
The above answer, strrev reverses the entire string. To reverse the order of the words:
$str = 'Hello everybody in stackoverflow';
$tmp = explode(' ', $str);
$tmp = array_reverse($tmp);
$reversed_str = join(' ', $tmp);
$tmp = explode(' ', $string);
array_reverse($tmp);
$string = implode(' ', $tmp);
In prose that is:
$words = explode(' ', $string);
$reversed_string = implode(' ', array_reverse($words));
Reading the whole list of string and array functions in PHP is VERY helpful and will save tons of time.