删除括号之间的文本PHP

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

范例:

ABC(测试1)

我想删除(Test1),只离开ABC

谢谢


www说
浏览 730回答 3
3回答

白衣染霜花

$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

BIG阳

$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。
打开App,查看更多内容
随时随地看视频慕课网APP