Search code examples
phpregexstringpreg-replacespace

how to define regex (preg_replace) to removes space between number character


I have string like this:

$str = "old iccid : 809831 3245 345 new iccid : 999000 112221"

How to define regex in order to remove the space character between number character in PHP, to become this output?

$output = "old iccid : 8098313245345 new iccid : 999000112221";

i've tried to use this syntax:

$output = preg_replace( '/\d[ *]\d/', '', $str);

but didn't work well.


Solution

  • Try:

    $output = preg_replace('/(?<=\d)\s+(?=\d)/', '', $str);