Skip to content
Advertisement

preg_replace get each two characters from a word

Actually I’m trying to come up with a way of getting a string like “GEWO” out of the “Get Word” by using preg_replace function. The thing is there also can be a strings like “Get”, or “Get Word And another Word” so I have to get the proper string respectively.

Thanks!

Advertisement

Answer

Use this:

$s = "Get Word And another Word";
$s = preg_replace('/(?<=w{2})w*b(s|$)/','',$s);
$s = strtoupper($s);
echo $s;

ouput: GEWOANANWO

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement