The Codesee The Codesee - 3 months ago 17
PHP Question

Only remove numbers inside parentheses

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 so that the output is
Number123()
?

Answer

Include the parentheses in the pattern, like so:

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

Output:

Number123()

Comments