0

0

php 过滤html标记属性类(附源码)

php中文网

php中文网

发布时间:2016-07-25 08:55:20

|

1121人浏览过

|

来源于php中文网

原创

  1. /** html attribute filter

  2. * date: 2013-09-22
  3. * author: fdipzone
  4. * ver: 1.0
  5. * edit: bbs.it-home.org
  6. * func:
  7. * public strip 过滤属性
  8. * public setallow 设置允许的属性
  9. * public setexception 设置特例
  10. * public setignore 设置忽略的标记
  11. * private findelements 搜寻需要处理的元素
  12. * private findattributes 搜寻属性
  13. * private removeattributes 移除属性
  14. * private isexception 判断是否特例
  15. * private createattributes 创建属性
  16. * private protect 特殊字符转义
  17. */
  18. class htmlattributefilter{ // class start
  19. private $_str = ''; // 源字符串
  20. private $_allow = array(); // 允许保留的属性 例如:array('id','class','title')
  21. private $_exception = array(); // 特例 例如:array('a'=>array('href','class'),'span'=>array('class'))
  22. private $_ignore = array(); // 忽略过滤的标记 例如:array('span','img')
  23. /** 处理html,过滤不保留的属性
  24. * @param string $str 源字符串
  25. * @return string
  26. */
  27. public function strip($str){
  28. $this->_str = $str;
  29. if(is_string($this->_str) && strlen($this->_str)>0){ // 判断字符串
  30. $this->_str = strtolower($this->_str); // 转成小写
  31. $res = $this->findelements();
  32. if(is_string($res)){
  33. return $res;
  34. }
  35. $nodes = $this->findattributes($res);
  36. $this->removeattributes($nodes);
  37. }
  38. return $this->_str;
  39. }
  40. /** 设置允许的属性
  41. * @param array $param
  42. */
  43. public function setallow($param=array()){
  44. $this->_allow = $param;
  45. }
  46. /** 设置特例
  47. * @param array $param
  48. */
  49. public function setexception($param=array()){
  50. $this->_exception = $param;
  51. }
  52. /** 设置忽略的标记
  53. * @param array $param
  54. */
  55. public function setignore($param=array()){
  56. $this->_ignore = $param;
  57. }
  58. /** 搜寻需要处理的元素 */
  59. private function findelements(){
  60. $nodes = array();
  61. preg_match_all("/\n]+)([^>]*)>/i", $this->_str, $elements);
  62. foreach($elements[1] as $el_key => $element){
  63. if($elements[2][$el_key]){
  64. $literal = $elements[0][$el_key];
  65. $element_name = $elements[1][$el_key];
  66. $attributes = $elements[2][$el_key];
  67. if(is_array($this->_ignore) && !in_array($element_name, $this->_ignore)){
  68. $nodes[] = array('literal'=>$literal, 'name'=>$element_name, 'attributes'=>$attributes);
  69. }
  70. }
  71. }
  72. if(!$nodes[0]){
  73. return $this->_str;
  74. }else{
  75. return $nodes;
  76. }
  77. }
  78. /** 搜寻属性
  79. * @param array $nodes 需要处理的元素
  80. */
  81. private function findattributes($nodes){
  82. foreach($nodes as &$node){
  83. preg_match_all("/([^ =]+)\s*=\s*[\"|']{0,1}([^\"']*)[\"|']{0,1}/i", $node['attributes'], $attributes);
  84. if($attributes[1]){
  85. foreach($attributes[1] as $att_key=>$att){
  86. $literal = $attributes[0][$att_key];
  87. $attribute_name = $attributes[1][$att_key];
  88. $value = $attributes[2][$att_key];
  89. $atts[] = array('literal'=>$literal, 'name'=>$attribute_name, 'value'=>$value);
  90. }
  91. }else{
  92. $node['attributes'] = null;
  93. }
  94. $node['attributes'] = $atts;
  95. unset($atts);
  96. }
  97. return $nodes;
  98. }
  99. /** 移除属性
  100. * @param array $nodes 需要处理的元素
  101. */
  102. private function removeattributes($nodes){
  103. foreach($nodes as $node){
  104. $node_name = $node['name'];
  105. $new_attributes = '';
  106. if(is_array($node['attributes'])){
  107. foreach($node['attributes'] as $attribute){
  108. if((is_array($this->_allow) && in_array($attribute['name'], $this->_allow)) || $this->isexception($node_name, $attribute['name'], $this->_exception)){
  109. $new_attributes = $this->createattributes($new_attributes, $attribute['name'], $attribute['value']);
  110. }
  111. }
  112. }
  113. $replacement = ($new_attributes) ? "" : "";
  114. $this->_str = preg_replace('/'.$this->protect($node['literal']).'/', $replacement, $this->_str);
  115. }
  116. }
  117. /** 判断是否特例
  118. * @param string $element_name 元素名
  119. * @param string $attribute_name 属性名
  120. * @param array $exceptions 允许的特例
  121. * @return boolean
  122. */
  123. private function isexception($element_name, $attribute_name, $exceptions){
  124. if(array_key_exists($element_name, $this->_exception)){
  125. if(in_array($attribute_name, $this->_exception[$element_name])){
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. /** 创建属性

  132. * @param String $new_attributes
  133. * @param String $name
  134. * @param String $value
  135. * @return String
  136. */
  137. private function createAttributes($new_attributes, $name, $value){
  138. if($new_attributes){
  139. $new_attributes .= " ";
  140. }
  141. $new_attributes .= "$name=\"$value\"";
  142. return $new_attributes;
  143. }
  144. /** 特殊字符转义
  145. * @param String $str 源字符串
  146. * @return String
  147. */
  148. private function protect($str){
  149. $conversions = array(
  150. "^" => "\^",
  151. "[" => "\[",
  152. "." => "\.",
  153. "$" => "\$",
  154. "{" => "\{",
  155. "*" => "\*",
  156. "(" => "\(",
  157. "\\" => "\\\\",
  158. "/" => "\/",
  159. "+" => "\+",
  160. ")" => "\)",
  161. "|" => "\|",
  162. "?" => "\?",
  163. " "\ ">" => "\>"
  164. );
  165. return strtr($str, $conversions);
  166. }
  167. } // class end
  168. ?>
复制代码

2,演示示例

  1. require('HtmlAttributeFilter.class.php');
  2. $str = '
    ';
  3. $obj = new HtmlAttributeFilter();
  4. // 允许id属性
  5. $obj->setAllow(array('id'));
  6. $obj->setException(array(
  7. 'a' => array('href'), // a 标签允许有 href属性特例
  8. 'ul' => array('class') // ul 标签允许有 class属性特例
  9. ));
  10. // img 标签忽略,不过滤任何属性
  11. $obj->setIgnore(array('img'));
  12. echo 'source str:
    ';
  13. echo htmlspecialchars($str).'

    ';
  14. echo 'filter str:
    ';
  15. echo htmlspecialchars($obj->strip($str));
  16. ?>
复制代码

附,php 过滤html标记属性类的源码下载地址



相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

65

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

43

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

35

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

41

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

204

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

9

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

8

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

3

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号