Page 1 of 1

Find multiple matches with regex in php string

Posted: 17 Mar 2019, 16:21
by Marius
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?

Find multiple matches with regex in php string

Posted: 17 Mar 2019, 16:24
by Admin
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)