【问题标题】:Replace content between two tags替换两个标签之间的内容
【发布时间】:2014-11-08 14:32:44
【问题描述】:

我想用 php 替换 () 之间的所有内容。

我的字符串:

$string = "This is a (string)";

所需的输出是:

$string = "This is a";

我的代码不起作用:

$string = "This is a (string)";
$search = "/[^(](.*)[^)]/";
$string = preg_replace($search, "", $string);
echo $string;  // output is ")"

【问题讨论】:

    标签: php replace preg-replace


    【解决方案1】:
    $result = preg_replace('/\(.+?\)/sm', 'Put your text here', $string);
    

    试试这个代码

    或者保存()把它们添加到替换

    $page = preg_replace('/\(.+?\)/sm', '(Put your text here)', $page);
    

    【讨论】:

      【解决方案2】:

      这应该适合你:

      <?php
      
          $string = "This is a (string)";
          echo preg_replace("/\([^)]+\)/","",$string);
      
      ?>
      

      输出:

      This is a
      

      【讨论】:

        【解决方案3】:

        只需改变它:

        $search = "/ *\(.*?\)/";
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-29
          • 2018-01-06
          • 2022-01-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多