从html标记中删除所有属性 [英] Remove all attributes from an html tag

查看:68
本文介绍了从html标记中删除所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个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细分:

/              # 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>,它应删除标记名之后的所有文本,直到标记/>>的末尾.

Add some quoting, and use the replacement text <$1$2> it should strip any text after the tagname until the end of tag /> or just >.

请注意,这不一定适用于 ALL 输入,因为Anti-HTML + RegExp会告诉您.有一些后备功能,最明显的是<p style=">">会以<p>">结尾,还有其他一些坏的问题...我建议您查看

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 <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

这篇关于从html标记中删除所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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