Php return line

What would be the best way to change the order of the string, e.g.

'Hello everybody in stackoverflow'

becomes

'stackoverflow in everybody Hello'

any ideas

+3
source share
4 answers

Try the following:

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

The above strrev answer changes the whole line. To change the word order:

$str = 'Hello everybody in stackoverflow';
$tmp = explode(' ', $str);
$tmp = array_reverse($tmp);
$reversed_str = join(' ', $tmp);
+1
source
$tmp = explode(' ', $string);
array_reverse($tmp);
$string = implode(' ', $tmp);
+1
source

In prose, which is:

  • First translate the string into an array of words

$ words = explode ('', $ string);

  • Secondly, we invert the order of elements in this array

$ reverseed_string = implode ('', array_reverse ($ words));

Reading the entire list of string and array functions in PHP is VERY useful and will save a ton of time.

+1
source

Source: https://habr.com/ru/post/1705885/


All Articles