删除括号之间的文本 PHP

发布于 2024-08-19 23:46:06 字数 119 浏览 10 评论 0原文

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

示例:

ABC (Test1)

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

吐个泡泡 2024-08-26 23:46:07
$string = "ABC (Test1)";
echo preg_replace("/\([^)]+\)/","",$string); // 'ABC '

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

正则表达式细分:

/  - 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
$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
清风夜微凉 2024-08-26 23:46:07

接受的答案对于非嵌套括号非常有用。对正则表达式的轻微修改使其可以处理嵌套括号。

$string = "ABC (Test1(even deeper) yes (this (works) too)) outside (((ins)id)e)";
echo preg_replace("/\(([^()]*+|(?R))*\)/","", $string);

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);
独自唱情﹋歌 2024-08-26 23:46:07

没有正则表达式

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

without regex

$string="ABC (test)"
$s=explode("(",$string);
print trim($s[0]);
心奴独伤 2024-08-26 23:46:07
$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);

它的工作原理是循环遍历每个字符,计算括号的数量。仅当 $paren_num == 0(当它位于所有括号之外时)才会将字符附加到我们的结果字符串 $new_string 中。

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

日暮斜阳 2024-08-26 23:46:07

最快速的方法(不带 preg):

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

如果您不想修剪单词末尾的空格,只需从代码中删除修剪功能即可。

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.

走过海棠暮 2024-08-26 23:46:07
$str ="ABC (Test1)";    
echo preg_replace( '~\(.*\)~' , "", $str );      
$str ="ABC (Test1)";    
echo preg_replace( '~\(.*\)~' , "", $str );      
佼人 2024-08-26 23:46:07

各位,正则表达式不能用于解析非正则语言。非常规语言是那些需要状态来解释的语言(即记住当前有多少个括号)。

上述所有答案都将在此字符串上失败:“ABC (hello (world) how are you)”。

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文