【发布时间】:2018-03-01 04:37:33
【问题描述】:
我想删除字符串中 () 内的所有内容。例如,如果我的字符串是$string = "this is an example string (this is the part i want to remove)";
我用 str_replace 尝试过,但它自然只是替换了“(”而不是里面的内容。我怎么能这样做,因为我事先不知道内容呢?
【问题讨论】:
-
稍作研究会有所帮助。
我想删除字符串中 () 内的所有内容。例如,如果我的字符串是$string = "this is an example string (this is the part i want to remove)";
我用 str_replace 尝试过,但它自然只是替换了“(”而不是里面的内容。我怎么能这样做,因为我事先不知道内容呢?
【问题讨论】:
这可能有效
preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $String);
【讨论】:
[、.、( 和 ) 在字符类中没有特殊含义。在这种情况下,它们不需要被转义。
$string = "this is an example string (this is the part i want to remove)";
$string2=preg_replace("/\([^)]+\)/","",$string);
试试这个
【讨论】: