Search code examples
phpregexpreg-replacepcreparentheses

Remove all opening and closing parentheses in a string


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

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


Solution

  • 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('(' => '', ')' => ''));