preg_replace 删除空标签但保留块引号的末尾

发布于 2024-08-14 09:44:02 字数 801 浏览 2 评论 0原文

我创建这个表达式是为了删除页面中的所有空标签(包括只有空格的标签)。

$content =  preg_replace('/<[^\/>]*>([\s]?)*<\/[^>]*>/', '', $content);

它工作得很好,直到它必须处理这样的内容...

 <blockquote>
<p >foo bar</p>
</blockquote>
<p ><a href="image.jpg" rel="lightbox" title=""><img  title="image" src="image.jpg" /></a><br /></p>

并将其输出为...

<blockquote>
<p >this is a test for the pluggin</p>
<p ><a href="image.jpg" rel="lightbox" title=""><img  title="image" src="image.jpg" /></a><br /></p>

从而删除

我一直在摸索这个问题,但无法让它发挥作用。除了指定应该格式化哪些标签之外,任何人都可以看到明显的解决方案吗?我还应该说它正在格式化 WordPress 帖子上的“the_content”。

I made this expression to remove all empty (inluding tags with just whitespace) tags in the page.

$content =  preg_replace('/<[^\/>]*>([\s]?)*<\/[^>]*>/', '', $content);

It worked a treat until it had to deal with content like this...

 <blockquote>
<p >foo bar</p>
</blockquote>
<p ><a href="image.jpg" rel="lightbox" title=""><img  title="image" src="image.jpg" /></a><br /></p>

and it outputs it as...

<blockquote>
<p >this is a test for the pluggin</p>
<p ><a href="image.jpg" rel="lightbox" title=""><img  title="image" src="image.jpg" /></a><br /></p>

Thus removing the </blockquote>.

I have been scratching my head on this one and can't get it working. Can anyone see an obvious solution other than specifying what tags it should format? I should also say that it is formatting 'the_content' on a wordpress post.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑看君怀她人 2024-08-21 09:44:02

正则表达式和 HTML 并不是一个很好的匹配,因为 HTML 不是常规语法,并且存在无穷无尽的边缘情况和陷阱。您最好使用 HTML 解析器(例如 这个)并检查/操作 DOM 对象。

Regexps and HTML are not a good match, since HTML is not a regular syntax, and there are no end of edge cases and gotchas. You'll be better off using an HTML parser such as this one and inspecting/manipulating the DOM object.

嘿嘿嘿 2024-08-21 09:44:02

您可能还想看看 HTML Purifier,如果您发现它,它比 Simple HTML Dom 更高级没有获得所有标签。

You might also like to take a look at HTML Purifier, which is more advanced than Simple HTML Dom, if you find it doesn't get all the tags.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文