3 回答
![?](http://img1.sycdn.imooc.com/54584f8f00019fc002200220-100-100.jpg)
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中更完整的证明标签/属性过滤器
- 3 回答
- 0 关注
- 848 浏览
添加回答
举报