欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-04-19 17:13:26
...
  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标记属性类的源码下载地址