您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

从html标记中删除所有属性

从html标记中删除所有属性

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

$text = '<p style="padding:0px;"><strong style="padding:0;margin:0;">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 style=">"><p>">失败,还有其他一些坏的问题…我建议将Zend_Filter_StripTags视为PHP中更全面的标签/属性过滤器

其他 2022/1/1 18:16:46 有627人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶