Find multiple matches with regex in php string

Discuss coding issues, and scripts related to PHP and MySQL.
Marius
Posts: 107

Find multiple matches with regex in php string

I have this string: 'mr (3_22) mrs (1_12) miss (2_4)'
I want php to output that string to: (3_22)(1_12)(2_4)
How can i do that with php?

Admin Posts: 805
You can use preg_match_all(). Try the following code:

Code: Select all

$str ='mr (3_22) mrs (1_12) miss (2_4)';
$re ='';
if(preg_match_all('/(\([^\)]+\))/i', $str, $mt)){
  $re = implode('', $mt[0]);
}
echo $re; // (3_22)(1_12)(2_4)

Similar Topics