PHP: Preg replace parentheses?

Norse picture Norse · Mar 1, 2012 · Viewed 18.4k times · Source

What is the proper syntax to preg_replace just the parenthesis in PHP?

$search = preg_replace('/\(\)/','',$search);

Thank you

Answer

J. Bruni picture J. Bruni · Mar 1, 2012

Assuming you want to remove both ( and ) from the $search string:

$search = preg_replace('/\(|\)/','',$search);

I think the fastest way to do this is using the strtr function, like this:

$search = strtr($search, array('(' => '', ')' => ''));