Delete text between PHP brackets

I'm just wondering how can I remove text between brackets and brackets in php.

Example:

ABC (Test1)

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

thank

+42
php text parentheses
Feb 01 '10 at 2:45
source share
7 answers
$string = "ABC (Test1)"; echo preg_replace("/\([^)]+\)/","",$string); // 'ABC ' 

preg_replace is the usual perl-based regular expression replacement routine. What this script does is match all occurrences of the opening bracket, followed by any number of characters, not the closing bracket, and the closing bracket again, and then remove them:

Regular Expression Distribution:

 / - 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 
+112
Feb 01 '10 at 2:46
source share
โ€” -

without regular expression

 $string="ABC (test)" $s=explode("(",$string); print trim($s[0]); 
+11
Feb 01 2018-10-02T00
source share
 $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 over each character counting parentheses. Only when $paren_num == 0 (when it is outside all parentheses) does it add characters to our resulting string, $new_string .

+9
May 30 '12 at
source share

The accepted answer is great for non-nested parentheses. A small change in the regular expression allows you to work with nested brackets.

 $string = "ABC (Test1(even deeper) yes (this (works) too)) outside (((ins)id)e)"; echo preg_replace("/\(([^()]*+|(?R))*\)/","", $string); 
+5
Aug 03 '15 at 13:06
source share

People, regular expressions CANNOT be used to analyze irregular languages. Incorrect languages โ€‹โ€‹are those that require an interpretation of the state (i.e., Remember how many brackets are currently open).

All of the above answers will not be executed on this line: "ABC (hello (world), how are you)".

Read the Jeff Atwood Parsing Html Cthulhu Path: http://www.codinghorror.com/blog/archives/001311.html , and then use either a manual written parser (loop through characters in a line, see if a character is a bracket or not , support the stack) or use lexer / parser, capable of analyzing a context-free language.

Also see this wikipedia article โ€œthe language of correctly matched brackets: http://en.wikipedia.org/wiki/Dyck_language

+4
Feb 01 '10 at 7:09
source share

Most quik methods (without preg):

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

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

0
Oct 19 '16 at 12:28
source share
 $str ="ABC (Test1)"; echo preg_replace( '~\(.*\)~' , "", $str ); 
0
Jul 06 '17 at 8:56 on
source share



All Articles