如何删除空的html标记(包含空格和/或其html代码) [英] How to remove empty html tags (which contain whitespaces and/or their html codes)
本文介绍了如何删除空的html标记(包含空格和/或其html代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要preg_replace的正则表达式.
Need a regex for preg_replace.
该问题未在另一个问题"中回答因为并非我要删除的所有标签都不都是空的.
This question wasn't answered in "another question" because not all tags I want to remove aren't empty.
我不仅要从HTML结构中删除空标签,还必须删除包含换行符,空格和/或其html代码的标签.
I have not only to remove empty tags from an HTML structure, but also tags containing line breaks as well as white spaces and/or their html code.
可能的代码是:
< br/> & nbsp; & thinsp; & ensp; & emsp;      
<br />            
在删除匹配标签之前:
<div>
<h1>This is a html structure.</h1>
<p>This is not empty.</p>
<p></p>
<p><br /></p>
<p> <br /> &;thinsp;</p>
<p> </p>
<p> </p>
</div>
在删除匹配的标签之后:
AFTER removing matching tags:
<div>
<h1>This is a html structure.</h1>
<p>This is not empty.</p>
</div>
推荐答案
您可以使用以下内容:
<([^>\s]+)[^>]*>(?:\s*(?:<br \/>| | | | | | | )\s*)*<\/\1>
并替换为''
(空字符串)
请参见演示
注意:这也适用于具有属性的空html标签.
Note: This will also work for empty html tags with attributes.
这篇关于如何删除空的html标记(包含空格和/或其html代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文