Remove Text Between Parentheses PHP

PhpTextParentheses

Php Problem Overview


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

Php Solutions


Solution 1 - Php

$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

Solution 2 - Php

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);

Solution 3 - Php

without regex

$string="ABC (test)"
$s=explode("(",$string);
print trim($s[0]);

Solution 4 - Php

$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.

Solution 5 - Php

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.

Solution 6 - Php

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

Solution 7 - Php

$str ="ABC (Test1)";    
echo preg_replace( '~\(.*\)~' , "", $str );      

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBelgin FishView Question on Stackoverflow
Solution 1 - PhpcmptrgeekkenView Answer on Stackoverflow
Solution 2 - PhpTegan SnyderView Answer on Stackoverflow
Solution 3 - Phpghostdog74View Answer on Stackoverflow
Solution 4 - PhptyjkennView Answer on Stackoverflow
Solution 5 - PhpMERT DOĞANView Answer on Stackoverflow
Solution 6 - PhpAlex WeinsteinView Answer on Stackoverflow
Solution 7 - PhpNagaView Answer on Stackoverflow