【问题标题】:How can i remove <div> tag from variable without using strip_tags?如何在不使用 strip_tags 的情况下从变量中删除 <div> 标签?
【发布时间】:2012-09-11 18:48:56
【问题描述】:

这是我的代码

$str="<div>this is the variable</div>";

我想在不使用 strip_tags 的情况下删除它的 html 标签&lt;div&gt;。 我需要$str="this is the variable" 因为我的服务器没有使用 strip_tags。 我知道 preg_replace 是可能的。但我不知道正则表达式。 所以请给我建议解决方案。

提前谢谢你。

【问题讨论】:

  • 除非您使用的是 PHP 3(我希望 noone 是这样),否则 strip_tags 应该可以工作....
  • $str = preg_replace("&lt;div&gt;", "", "&lt;div&gt;this is the variable&lt;/div&gt;"); $str = preg_replace("&lt;/div&gt;", "", "&lt;div&gt;this is the variable&lt;/div&gt;"); php.net/manual/en/function.preg-replace.php
  • @TWCrap 不,不要! $str = preg_replace("?div>","",$str);

标签: php regex html


【解决方案1】:
$s = "<div>this is the variable</div>";
echo preg_replace("/<div>(.*?)<\/div>/", "$1", $s);
// this is the variable

当然,不要使用正则表达式解析 HTML :-)

DEMO.

【讨论】:

  • 感谢 Joa Silva 的回答。但令我惊讶的是,您的代码在我的本地运行良好,但在我的服务器上运行良好。而且我也不明白“当然,不要用正则表达式解析 HTML”
  • @KrishnaKarki:一般不建议使用正则表达式解析 HTML;通常最好使用 HTML 解析器。但是,如果您只想提取 div 之间的文本,并且它具有 static 结构,则可以使用正则表达式。话虽如此,奇怪的是在本地而不是在您的服务器上工作。你确定字符串是一样的吗?
  • 是的,字符串是一样的。我刚刚复制了您的代码并粘贴到我的项目中进行测试。
  • @KrishnaKarki:你对生产代码中的结果做了什么? echoing 吗?将其保存在变量中,即$result = preg_replace(...)? PHP版本是一样的吗?
  • 我在呼应这个变量。我的 PHP 版本是 5.2.17
【解决方案2】:

如果你只关注这样的 div 标签,你可以试试

preg_replace("</?div>","",$string);

当然有人可能会提到not to parse html with regex

【讨论】:

    【解决方案3】:

    你可以这样做:

    $str="<div>this is the variable</div>";
    $str = preg_replace('/<(.*?)>/i', '', $str);
    

    这将删除所有 html 标签,而不仅仅是 &lt;div&gt;

    【讨论】:

      【解决方案4】:

      如果 DIV 标记包含属性,此变体也适用:

      $str = '<div id="some_id">this is the variable</div>';
      $str = preg_replace('/\<[\/]{0,1}div[^\>]*\>/i', '', $str);
      echo $str;
      

      【讨论】:

      • $str = preg_replace('/^\]*\>/i', '', $str);使用 ^ 以便它只删除第一个 div
      【解决方案5】:

      如果您想删除文本周围的标签并保留其他标签,请使用:

      $node = new DOMDocument();
      $str = $node->loadXML($str)->firstChild->textContent;
      

      警告:这只会删除第一个包装标签

      这会将 HTML 解析为标记,它就是这样。

      【讨论】:

        【解决方案6】:

        使用

        preg_replace("#<div>(.*?)</div>#s", "$1", $s);
        

        s - 一个 DOTALL 修饰符,使 . 匹配换行符。

        # - 与/ 不同的正则表达式分隔符,感谢它们,无需转义/

        解释

        --------------------------------------------------------------------------------
          <div>                    '<div>'
        --------------------------------------------------------------------------------
          (                        group and capture to \1:
        --------------------------------------------------------------------------------
            .*?                      any character (0 or more times, the least amount possible))
        --------------------------------------------------------------------------------
          )                        end of \1
        --------------------------------------------------------------------------------
          </div>                   '</div>'
        

        $1 是对用括号内的第一组捕获的文本的反向引用

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-20
          • 1970-01-01
          • 2021-08-16
          • 2021-05-20
          相关资源
          最近更新 更多