Could anyone please help me with the regex of reversing each word in a string?
Sample input:
Hello how are you my friend?
Desired Output
olleH woh era uoy ym ?dneirf
I want to implement this in PHP.
Could anyone please help me with the regex of reversing each word in a string?
Sample input:
Hello how are you my friend?
Desired Output
olleH woh era uoy ym ?dneirf
I want to implement this in PHP.
This gives you almost what you want.
If it isn't close enough, look into Blender's solution of exploding on spaces.
preg_replace_callback('/\b(\w+)\b/', function($match) {
return strrev($match[1]);
}, $str);
You can reverse text using plain regex (and some string functions) but it is ugly...
$length = strlen($str);
echo preg_replace(
'/' . str_repeat('(.)', $length) . '/s',
'$' . join('$', range($length, 1)),
$str
);
The code sample above is for demonstration only. Please do not ever use it :)