74

I'm just wondering how I could remove the text between a set of parentheses and the parentheses themselves in php.

Example :

ABC (Test1)

I would like it to delete (Test1) and only leave ABC

Thanks

antonjs
  • 14,060
  • 14
  • 65
  • 91
Belgin Fish
  • 803
  • 2
  • 7
  • 7

7 Answers7

193
$string = "ABC (Test1)";
echo preg_replace("/\([^)]+\)/","",$string); // 'ABC '

preg_replace is a perl-based regular expression replace routine. What this script does is matches all occurrences of a opening parenthesis, followed by any number of characters not a closing parenthesis, and again followed by a closing parenthesis, and then deletes them:

Regular expression breakdown:

/  - opening delimiter (necessary for regular expressions, can be any character that doesn't appear in the regular expression
\( - Match an opening parenthesis
[^)]+ - Match 1 or more character that is not a closing parenthesis
\) - Match a closing parenthesis
/  - Closing delimiter
cmptrgeekken
  • 8,052
  • 3
  • 29
  • 35
  • You shouldn't try and escape the ) in your character class. – mopoke Feb 01 '10 at 02:50
  • 3
    unfortunately this does not remove nested brackets such as `A (B (C) D)` and leaves you with `A D` – David K. Jun 12 '14 at 11:26
  • If one is dealing with text (like removing this from the middle of my comment) - then this function is fine. Nested brackets are generally not seen in text strings, at least not in English, but in mathematical equations. – Jeff Clayton Feb 15 '18 at 13:55
  • @cmptrgeekken Thank you for your brief summary :) – Jean Manzo Mar 03 '19 at 20:47
22

The accepted answer works great for non-nested parentheses. A slight modification to the regex allows it to work on nested parentheses.

$string = "ABC (Test1(even deeper) yes (this (works) too)) outside (((ins)id)e)";
echo preg_replace("/\(([^()]*+|(?R))*\)/","", $string);
Tegan Snyder
  • 785
  • 7
  • 12
  • I like this one! Don't forget to replace the potential extra whitespaces that might get left behind in your result string though – Kalle Nov 09 '18 at 08:43
  • This is by far the best solution I've found. I was also curious what would happen if I accidentally left a stray "(". And it did what I thought it would do: just leave it alone. A little extra text showed up as a result, but it didn't break anything. I'm happy. – Matthew Campbell Apr 12 '21 at 21:28
13
$string = "ABC (Test1(even deeper) yes (this (works) too)) outside (((ins)id)e)";
$paren_num = 0;
$new_string = '';
foreach($string as $char) {
    if ($char == '(') $paren_num++;
    else if ($char == ')') $paren_num--;
    else if ($paren_num == 0) $new_string .= $char;
}
$new_string = trim($new_string);

It works by looping through each character, counting parentheses. Only when $paren_num == 0 (when it is outside all parentheses) does it append the characters to our resulting string, $new_string.

tyjkenn
  • 709
  • 10
  • 25
13

without regex

$string="ABC (test)"
$s=explode("(",$string);
print trim($s[0]);
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • This works for "ABC (test)" but fails bad on "ABC (test) DEF (harder test)" where it gets very very confused. – ftrotter Aug 17 '18 at 02:29
7

Most quik method (without preg):

$str='ABC (TEST)';
echo trim(substr($str,0,strpos($str,'(')));

If you don't want to trim spaces at end of word, just remove trim function from code.

MERT DOĞAN
  • 2,864
  • 26
  • 28
4

Folks, regular expressions CANNOT be used to parse non-regular languages. Non-regular languages are those that require state to interpret (i.e. remembering how many parenthesis are currently open).

All of the above answers will fail on this string: "ABC (hello (world) how are you)".

Read Jeff Atwood's Parsing Html The Cthulhu Way: https://blog.codinghorror.com/parsing-html-the-cthulhu-way/, and then use either a by-hand written parser (loop through the characters in the string, see if the character is a parenthesis or not, maintain a stack) or use a lexer/parser capable of parsing a context-free language.

Also see this wikipedia article on the "language of properly matched parenthesis:" https://en.wikipedia.org/wiki/Dyck_language

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alex Weinstein
  • 9,823
  • 9
  • 42
  • 59
4
$str ="ABC (Test1)";    
echo preg_replace( '~\(.*\)~' , "", $str );      
Naga
  • 2,190
  • 3
  • 16
  • 21