【问题标题】:PHP - Remove everything inside () in a string [duplicate]PHP - 删除字符串中()内的所有内容[重复]
【发布时间】:2018-03-01 04:37:33
【问题描述】:

我想删除字符串中 () 内的所有内容。例如,如果我的字符串是$string = "this is an example string (this is the part i want to remove)";

我用 str_replace 尝试过,但它自然只是替换了“(”而不是里面的内容。我怎么能这样做,因为我事先不知道内容呢?

【问题讨论】:

标签: php string


【解决方案1】:

这可能有效

preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $String);

【讨论】:

  • [.() 在字符类中没有特殊含义。在这种情况下,它们不需要被转义。
  • “可能有用”?那有什么用??
  • 我没有得到他想要的,所以我把我理解的给了他。
  • @Mynameisjeff 你到底有什么不明白的??大声笑
  • @MysteriousDoorknob 你的问题不清楚
【解决方案2】:
$string = "this is an example string (this is the part i want to remove)";
$string2=preg_replace("/\([^)]+\)/","",$string);

试试这个

【讨论】:

    最近更新 更多