Search code examples
phpsubstringreverse

Reverse the direction of all sequences of letters in a string


Is it possible to reverse only the letters in a string?

For instance if I have the word $word = "word,!";

I need the result to be $result = "drow,!";

I tried using strrev, but it reverses the punctuation marks too.


Solution

  •  $word = "word,!pineapple--pizza";
     $revd = preg_replace_callback('#([A-Za-z]+)#', 'rev_first', $word);
    
    function rev_first($matches){
        return strrev($matches[1]);
    }
    

    http://3v4l.org/PXEs8

    Is this what you're looking for?