phpstringreverse

Reverse the letters in each word of a string


I have a string containing space-delimited words.

I want to reverse the letters in every word without reversing the order of the words.

I would like my string to become ym gnirts.


Solution

  • This should work:

    $words = explode(' ', $string);
    $words = array_map('strrev', $words);
    echo implode(' ', $words);
    

    Or as a one-liner:

    echo implode(' ', array_map('strrev', explode(' ', $string)));