删除括号之间的文本 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2174362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:21:38  来源:igfitidea点击:

Remove Text Between Parentheses PHP

phptextparentheses

提问by Belgin Fish

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

我只是想知道如何在 php.ini 中删除一组括号和括号之间的文本。

Example :

例子 :

ABC (Test1)

ABC(测试1)

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

我希望它删除(Test1)并且只留下ABC

Thanks

谢谢

回答by cmptrgeekken

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

preg_replaceis 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 nota closing parenthesis, and again followed by a closing parenthesis, and then deletes them:

preg_replace是一个基于 perl 的正则表达式替换例程。该脚本的作用是匹配所有出现的左括号,后跟任意数量的字符而不是右括号,再后跟右括号,然后删除它们:

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

回答by Tegan Snyder

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

回答by tyjkenn

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

它的工作原理是遍历每个字符,计算括号。只有当$paren_num == 0(当它在所有括号之外时)它才会将字符附加到我们的结果字符串$new_string.

回答by ghostdog74

without regex

没有正则表达式

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

回答by Alex Weinstein

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)".

以上所有答案都将在此字符串上失败:“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.

阅读 Jeff Atwood 的 Parsing Html The Cthulhu Way: https://blog.codinghorror.com/parsing-html-the-cthulhu-way/,然后使用手工编写的解析器(循环遍历字符串中的字符,请参阅如果字符是否为括号,则维护堆栈)或使用能够解析上下文无关语言的词法分析器/解析器。

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

另请参阅有关“正确匹配括号的语言”的维基百科文章:https: //en.wikipedia.org/wiki/Dyck_language

回答by MERT DO?AN

Most quik method (without preg):

大多数 quik 方法(不带 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.

如果您不想在词尾修剪空格,只需从代码中删除修剪功能。

回答by Naga

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