And*_*ndy 7 php regex ckeditor
我正在尝试删除<p>CKEditor插入描述框的所有空标签,但它们似乎都有所不同.可能性似乎是:
<p></p>
<p>(WHITESPACE)</p>
<p> </p>
<p><br /></p>
<p>(NEWLINE) </p>
<p>(NEWLINE)<br /><br />(NEWLINE) </p>
Run Code Online (Sandbox Code Playgroud)
有了这些可能性,段落之间可能会有任意数量的空格 和<br />标签,并且在一个段落中可能会有一些空格.
我也不确定<br />标签,从我看到它可能是<br />,<br/>或<br>.
我搜索过类似的答案,但是我看到的所有答案似乎都只适用于其中一种情况,而不是一次性完成.我想简单来说,我问的是,是否有一个正则表达式,我可以用来删除<p>某些HTML中没有任何字母数字文本或符号/标点符号的所有标签?
Cer*_*rus 17
好吧,与我的建议不要用正则表达式解析HTML相冲突,我写了一个正则表达式来做到这一点:
"#<p>(\s| |</?\s?br\s?/?>)*</?p>#"
Run Code Online (Sandbox Code Playgroud)
这将正确匹配:
<p></p>
<p> </p> <!-- ([space]) -->
<p> </p> <!-- (That's a [tab] character in there -->
<p> </p>
<p><br /></p>
<p>
</p>
<p>
<br /><br />
</p>
Run Code Online (Sandbox Code Playgroud)
它能做什么:
# / --> Regex start
# <p> --> match the opening <p> tag
# ( --> group open.
# \s --> match any whitespace character (newline, space, tab)
# | --> or
# --> match
# | --> or
# </?\s?br\s?/?> --> match the <br> tag
# )* --> group close, match any number of any of the elements in the group
# </?p> --> match the closing </p> tag ("/" optional)
# / --> regex end.
Run Code Online (Sandbox Code Playgroud)
所选的答案很好,但如果<p>标签定义了内联样式属性(例如<p style="font-weight:bold">.
匹配此的正则表达式将是:
#<p[^>]*>(\s| |</?\s?br\s?/?>)*</?p>#
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9696 次 |
| 最近记录: |