栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从html标记中删除所有属性

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从html标记中删除所有属性

改编自我对类似问题的回答

$text = '<p ><strong >hello</strong></p>';echo preg_replace("/<([a-z][a-z0-9]*)[^>]*?(/?)>/si",'<$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 '>'/is # End Pattern - Case Insensitive & Multi-line ability

添加一些引号,并使用替换文本,

<$1$2>
它应该删除标记名之后的所有文本,直到标记结尾
/>
或just 为止
>

请注意, 这不一定适用于 所有 输入,因为Anti-HTML + RegExp会告诉您。有一些后备功能,最明显的是

<p>
<p>">
失败,还有其他一些坏的问题…我建议将Zend_Filter_StripTags视为PHP中更全面的标签/属性过滤器



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470811.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号