为了账号安全,请及时绑定邮箱和手机立即绑定

从html标记中删除所有属性

从html标记中删除所有属性

PHP
宝慕林4294392 2019-08-16 17:18:23
从html标记中删除所有属性我有这个HTML代码:<p style="padding:0px;"><strong style="padding:0;margin:0;">hello</strong></p>但它应该成为(对于所有可能的html标签):<p><strong>hello</strong></p>
查看完整描述

3 回答

?
胡子哥哥

TA贡献1825条经验 获得超6个赞

$text = '<p style="padding:0px;"><strong style="padding:0;margin:0;">hello</strong></p>';


echo preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",'<$1$2>', $text);


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

/i            # End Pattern - Case Insensitive

添加一些引用,并使用替换文本,<$1$2>它应该删除标记名后面的任何文本,直到标记结束/>或只是>。


请注意这不一定适用于所有输入,因为Anti-HTML + RegExp会告诉您。有一些后退,最明显的是<p style=">">最终会<p>">和其他一些破坏的问题......我建议在Zend_Filter_StripTags中查看PHP中更完整的证明标签/属性过滤器


查看完整回答
反对 回复 2019-08-16
  • 3 回答
  • 0 关注
  • 848 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信