Only remove numbers inside parentheses (brackets)

2019-06-21 04:15发布

问题:

Using the code below, I can turn Number123(45) into Number.

$string = 'Number123(45)';
$string2 = preg_replace('/[0-9]+/', '', $string);
echo $string2;

How would I only remove the numbers inside parentheses (brackets) so that the output is Number123()?

回答1:

Include the parentheses in the pattern, like so:

$string = 'Number123(45)';
$string2 = preg_replace('/\([0-9]+\)/', '()', $string);
echo $string2;

Output:

Number123()