从html标记中删除所有属性

从html标记中删除所有属性

我有这个HTML代码:

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

但它应该成为(对于所有可能的html标签):

<p><strong>hello</strong></p>


宝慕林4294392
浏览 849回答 3
3回答

胡子哥哥

$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细分:/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Start Pattern&nbsp;<&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Match '<' at beginning of tags&nbsp;(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Start Capture Group $1 - Tag Name&nbsp; [a-z]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Match 'a' through 'z'&nbsp; [a-z0-9]*&nbsp; &nbsp; &nbsp;# Match 'a' through 'z' or '0' through '9' zero or more times&nbsp;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# End Capture Group&nbsp;[^>]*?&nbsp; &nbsp; &nbsp; &nbsp; # Match anything other than '>', Zero or More times, not-greedy (wont eat the /)&nbsp;(\/?)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Capture Group $2 - '/' if it is there&nbsp;>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Match '>'/i&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # End Pattern - Case Insensitive添加一些引用,并使用替换文本,<$1$2>它应该删除标记名后面的任何文本,直到标记结束/>或只是>。请注意这不一定适用于所有输入,因为Anti-HTML + RegExp会告诉您。有一些后退,最明显的是<p style=">">最终会<p>">和其他一些破坏的问题......我建议在Zend_Filter_StripTags中查看PHP中更完整的证明标签/属性过滤器
打开App,查看更多内容
随时随地看视频慕课网APP