【问题标题】:Error preg_replace remove all div inside content错误 preg_replace 删除所有 div 里面的内容
【发布时间】:2018-10-03 14:18:55
【问题描述】:
$content = '<div class="block_thumb_slide_show"><img alt="smartphone" class="aligncenter size-full">Test abc<p> <strong>Màn hình 3D</strong></p></div>';
$content = preg_replace('#<div.*?>(.*?)</div>#i', '<p>\1</p>', $content);
echo $content;

结果无法将 div 替换为 p。如何解决它

<p><img alt="smartphone" class="aligncenter size-full">Test abc<p> <strong>Màn hình 3D</strong></p></p>

【问题讨论】:

  • 使用preg_replace("/(&lt;/?)div/", "$1p", $content)
  • 预期回报是多少?

标签: php regex preg-replace


【解决方案1】:

如果你想用 p 替换 div 为什么不尝试使用:

str_replace("div","p", $your_html);

【讨论】:

  • 在这种情况下使用 str_replace 是不对的,因为它也替换了标签中的字符串
  • 如果他使用

    呢?

  • 如果 div 有属性 &lt;div class...&gt; 不起作用。也用于结束标签&lt;/div&gt;
  • true :D 我失败了 :D
  • 也许str_replace("&lt;div", "&lt;p", str_replace("&lt;/div", "&lt;/p", $content))工作
猜你喜欢
相关资源
最近更新 更多
热门标签