【问题标题】:Get rid of text in between () in PHP [duplicate]摆脱PHP中()之间的文本[重复]
【发布时间】:2013-03-31 09:21:16
【问题描述】:

假设你有一个这样的字符串:

This is a string (with parenthesis stuff)

你会怎么改成

This is a string

?

【问题讨论】:

    标签: php


    【解决方案1】:

    替换它:

    preg_replace('/\(.*?\)/', '', $str);
    

    【讨论】:

      【解决方案2】:

      使用正则表达式。

      用空字符串替换\([^)]*\)

      注意:如果有不止一对括号,这将替换最里面的一个。

      【讨论】:

        【解决方案3】:

        试试这个

          $string = "This is a string (with parenthesis stuff)";
          echo preg_replace("/\([^)]+\)/","",$string); // 'ABC '
        

        或者你也可以这样做

          $str = "This is a string (with parenthesis stuff)";
          $str = trim(preg_replace('/\s*\([^)]*\)/', '', $str));
        

        【讨论】:

          猜你喜欢
          • 2023-04-01
          • 1970-01-01
          • 2014-07-08
          • 1970-01-01
          • 2013-10-12
          • 1970-01-01
          • 2011-01-31
          • 2017-05-28
          • 1970-01-01
          相关资源
          最近更新 更多