正则表达式删除空的< span>标签 [英] Regular expression to remove empty <span> tags
问题描述
<跨度> &安培; NBSP; &安培; NBSP; &安培; NBSP; < / span>
我试过用这个正则表达式,但它需要调整:
(< span>(& nbsp; | \s)*< / span>)
preg_replace('#< span>(& nbsp; | \s)*< / span> #si','< \\\ 1>',
$ div class =h2_lin>解决方案
将Kent Fredric的正则表达式翻译成PHP:
preg_match_all('#< span [^>] *(?:/>> |>(?: \ s |& nbsp; )*< / span>)#im',$ html,$ result);
这将匹配:
也许您应该包含仅包含< br />
以及...
像往常一样,当涉及到调整正则表达式时,一些工具很方便:
http://regex.larsolavtorvik.com/
I would like such empty span tags (filled with
and space) to be removed:
<span> </span>
I've tried with this regex, but it needs adjusting:
(<span>( |\s)*</span>)
preg_replace('#<span>( |\s)*</span>#si','<\\1>',$encoded);
Translating Kent Fredric's regexp to PHP :
preg_match_all('#<span[^>]*(?:/>|>(?:\s| )*</span>)#im', $html, $result);
This will match :
- autoclosing spans
- spans on multilines and whatever the case
- spans with attributes
- span with unbreakable spaces
Maybe you should about including spans containings only <br />
as well...
As usual, when it comes to tweak regexp, some tools are handy :
http://regex.larsolavtorvik.com/
这篇关于正则表达式删除空的< span>标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!