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 1:

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)));

Solution 2:

echo implode(' ', array_reverse(explode(' ', strrev('my string'))));

This is considerably faster than reversing every string of the array after exploding the original string.

Solution 3:

Functionified:

<?php

function flipit($string){
    return implode(' ',array_map('strrev',explode(' ',$string)));
}

echo flipit('my string'); //ym gnirts

?>