使用Reg表达式删除空标签(其中的任何一个)? [英] Reg expression to remove empty Tags (any of them)?

查看:95
本文介绍了使用Reg表达式删除空标签(其中的任何一个)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢删除任何为空或包含空格的html标记.

I like to remove any empty html tag which is empty or containing spaces.

想要得到的东西:

$string = "<b>text</b><b><span> </span></b><p>  <br/></p><b></b><font size='4'></font>";

收件人:

$string ="<b>text</b>=;

推荐答案

这是使用

这将输出类似

<body><b>text</b></body>

XML文档需要一个根元素,因此无法忽略它.您可以str_replace.上面的代码可以处理损坏的HTML.

XML documents require a root element, so there is no way to omit that. You can str_replace it though. The above can handle broken HTML.

如果要有选择地删除特定节点,请调整XPath查询.

If you want to selectively remove specific nodes, adjust the XPath query.

也可以看到

  • How do you parse and process HTML/XML in PHP?
  • Locating the node by value containing whitespaces using XPath

这篇关于使用Reg表达式删除空标签(其中的任何一个)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆