gpt4 book ai didi

php - 去掉括号外的文字

转载 作者:可可西里 更新时间:2023-11-01 00:17:07 25 4
gpt4 key购买 nike

$text = 'remove this text (keep this text and 123)';

echo preg_replace('', '', $text);

它应该输出:

(keep this text and 123)

最佳答案

这会做到:(并且也适用于嵌套的 ())

$re = '/[^()]*+(\((?:[^()]++|(?1))*\))[^()]*+/';
$text = preg_replace($re, '$1', $text);

这里有几个测试用例:

Input:
Non-nested case: 'remove1 (keep1) remove2 (keep2) remove3'
Nested case: 'remove1 ((keep1) keep2 (keep3)) remove2'

Output:
Non-nested case: '(keep1)(keep2)'
Nested case: '(keep1) keep2 (keep3)'

关于php - 去掉括号外的文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5293283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com