Wednesday, December 29, 2021

[FIXED] Reverse the direction of all sequences of letters in a string

Issue

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?



Answered By - scragar

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.