从html标记中删除所有属性 [英] Remove all attributes from an html tag
问题描述
我有这个html代码:
i have this html code:
<p style="padding:0px;">
<strong style="padding:0;margin:0;">hello</strong>
</p>
但它应该变成(对于所有可能的html标签):
but it should become (for all possible html tags):
<p>
<strong>hello</strong>
</p>
推荐答案
改编自
RegExp细分: 添加一些引号,并使用替换文本 Add some quoting, and use the replacement text 请注意,这不一定适用于 ALL 输入,因为Anti-HTML + RegExp会告诉您.有一些后备功能,最明显的是 Please Note This isn't necessarily going to work on ALL input, as the Anti-HTML + RegExp will tell you. There are a few fallbacks, most notably 这篇关于从html标记中删除所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!/ # Start Pattern
< # Match '<' at beginning of tags
( # Start Capture Group $1 - Tag Name
[a-z] # Match 'a' through 'z'
[a-z0-9]* # Match 'a' through 'z' or '0' through '9' zero or more times
) # End Capture Group
[^>]*? # Match anything other than '>', Zero or More times, not-greedy (wont eat the /)
(\/?) # Capture Group $2 - '/' if it is there
> # Match '>'
/is # End Pattern - Case Insensitive & Multi-line ability
<$1$2>
,它应删除标记名之后的所有文本,直到标记/>
或>
的末尾.<$1$2>
it should strip any text after the tagname until the end of tag />
or just >
.<p style=">">
会以<p>">
结尾,还有其他一些坏的问题...我建议您查看<p style=">">
would end up <p>">
and a few other broken issues... I would recommend looking at Zend_Filter_StripTags as a more full proof tags/attributes filter in PHP