当前位置:Gxlcms > PHP教程 > php过滤html标记属性类

php过滤html标记属性类

时间:2021-07-01 10:21:17 帮助过:4人阅读

php 过滤html标记属性类


HtmlAttributeFilter.class.php

  1. <?php
  2. /** HTML Attribute Filter
  3. * Date: 2013-09-22
  4. * Author: fdipzone
  5. * ver: 1.0
  6. *
  7. * Func:
  8. * public strip 过滤属性
  9. * public setAllow 设置允许的属性
  10. * public setException 设置特例
  11. * public setIgnore 设置忽略的标记
  12. * private findElements 搜寻需要处理的元素
  13. * private findAttributes 搜寻属性
  14. * private removeAttributes 移除属性
  15. * private isException 判断是否特例
  16. * private createAttributes 创建属性
  17. * private protect 特殊字符转义
  18. */
  19. class HtmlAttributeFilter{ // class start
  20. private $_str = ''; // 源字符串
  21. private $_allow = array(); // 允许保留的属性 例如:array('id','class','title')
  22. private $_exception = array(); // 特例 例如:array('a'=>array('href','class'),'span'=>array('class'))
  23. private $_ignore = array(); // 忽略过滤的标记 例如:array('span','img')
  24. /** 处理HTML,过滤不保留的属性
  25. * @param String $str 源字符串
  26. * @return String
  27. */
  28. public function strip($str){
  29. $this->_str = $str;
  30. if(is_string($this->_str) && strlen($this->_str)>0){ // 判断字符串
  31. $this->_str = strtolower($this->_str); // 转成小写
  32. $res = $this->findElements();
  33. if(is_string($res)){
  34. return $res;
  35. }
  36. $nodes = $this->findAttributes($res);
  37. $this->removeAttributes($nodes);
  38. }
  39. return $this->_str;
  40. }
  41. /** 设置允许的属性
  42. * @param Array $param
  43. */
  44. public function setAllow($param=array()){
  45. $this->_allow = $param;
  46. }
  47. /** 设置特例
  48. * @param Array $param
  49. */
  50. public function setException($param=array()){
  51. $this->_exception = $param;
  52. }
  53. /** 设置忽略的标记
  54. * @param Array $param
  55. */
  56. public function setIgnore($param=array()){
  57. $this->_ignore = $param;
  58. }
  59. /** 搜寻需要处理的元素 */
  60. private function findElements(){
  61. $nodes = array();
  62. preg_match_all("/<([^ !\/\>\n]+)([^>]*)>/i", $this->_str, $elements);
  63. foreach($elements[1] as $el_key => $element){
  64. if($elements[2][$el_key]){
  65. $literal = $elements[0][$el_key];
  66. $element_name = $elements[1][$el_key];
  67. $attributes = $elements[2][$el_key];
  68. if(is_array($this->_ignore) && !in_array($element_name, $this->_ignore)){
  69. $nodes[] = array('literal'=>$literal, 'name'=>$element_name, 'attributes'=>$attributes);
  70. }
  71. }
  72. }
  73. if(!$nodes[0]){
  74. return $this->_str;
  75. }else{
  76. return $nodes;
  77. }
  78. }
  79. /** 搜寻属性
  80. * @param Array $nodes 需要处理的元素
  81. */
  82. private function findAttributes($nodes){
  83. foreach($nodes as &$node){
  84. preg_match_all("/([^ =]+)\s*=\s*[\"|']{0,1}([^\"']*)[\"|']{0,1}/i", $node['attributes'], $attributes);
  85. if($attributes[1]){
  86. foreach($attributes[1] as $att_key=>$att){
  87. $literal = $attributes[0][$att_key];
  88. $attribute_name = $attributes[1][$att_key];
  89. $value = $attributes[2][$att_key];
  90. $atts[] = array('literal'=>$literal, 'name'=>$attribute_name, 'value'=>$value);
  91. }
  92. }else{
  93. $node['attributes'] = null;
  94. }
  95. $node['attributes'] = $atts;
  96. unset($atts);
  97. }
  98. return $nodes;
  99. }
  100. /** 移除属性
  101. * @param Array $nodes 需要处理的元素
  102. */
  103. private function removeAttributes($nodes){
  104. foreach($nodes as $node){
  105. $node_name = $node['name'];
  106. $new_attributes = '';
  107. if(is_array($node['attributes'])){
  108. foreach($node['attributes'] as $attribute){
  109. if((is_array($this->_allow) && in_array($attribute['name'], $this->_allow)) || $this->isException($node_name, $attribute['name'],
  110. $this->_exception)){
  111. $new_attributes = $this->createAttributes($new_attributes, $attribute['name'], $attribute['value']);
  112. }
  113. }
  114. }
  115. $replacement = ($new_attributes) ? "<$node_name $new_attributes>" : "<$node_name>";
  116. $this->_str = preg_replace('/'.$this->protect($node['literal']).'/', $replacement, $this->_str);
  117. }
  118. }
  119. /** 判断是否特例
  120. * @param String $element_name 元素名
  121. * @param String $attribute_name 属性名
  122. * @param Array $exceptions 允许的特例
  123. * @return boolean
  124. */
  125. private function isException($element_name, $attribute_name, $exceptions){
  126. if(array_key_exists($element_name, $this->_exception)){
  127. if(in_array($attribute_name, $this->_exception[$element_name])){
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. /** 创建属性
  134. * @param String $new_attributes
  135. * @param String $name
  136. * @param String $value
  137. * @return String
  138. */
  139. private function createAttributes($new_attributes, $name, $value){
  140. if($new_attributes){
  141. $new_attributes .= " ";
  142. }
  143. $new_attributes .= "$name=\"$value\"";
  144. return $new_attributes;
  145. }
  146. /** 特殊字符转义
  147. * @param String $str 源字符串
  148. * @return String
  149. */
  150. private function protect($str){
  151. $conversions = array(
  152. "^" => "\^",
  153. "[" => "\[",
  154. "." => "\.",
  155. "$" => "\$",
  156. "{" => "\{",
  157. "*" => "\*",
  158. "(" => "\(",
  159. "\\" => "\\\\",
  160. "/" => "\/",
  161. "+" => "\+",
  162. ")" => "\)",
  163. "|" => "\|",
  164. "?" => "\?",
  165. "<" => "\<",
  166. ">" => "\>"
  167. );
  168. return strtr($str, $conversions);
  169. }
  170. } // class end
  171. ?>

demo

  1. <?php
  2. require('HtmlAttributeFilter.class.php');
  3. $str = '<p class="bd clearfix" id="index_hilite_ul"><ul class="list"><li>
  4. <img src="http://su.bdimg.com/static/skin/img/logo_white.png" width="118" height="148">
  5. <p class="cover"><a class="text" href="
  6. </a><strong class="t g">want to know</strong><a href="/login.html" class="ppBtn">
  7. <strong class="text">YES</strong></a></p></li></ul></p>';
  8. $obj = new HtmlAttributeFilter();
  9. // 允许id属性
  10. $obj->setAllow(array('id'));
  11. $obj->setException(array(
  12. 'a' => array('href'), // a 标签允许有 href属性特例
  13. 'ul' => array('class') // ul 标签允许有 class属性特例
  14. ));
  15. // img 标签忽略,不过滤任何属性
  16. $obj->setIgnore(array('img'));
  17. echo 'source str:<br>';
  18. echo htmlspecialchars($str).'<br><br>';
  19. echo 'filter str:<br>';
  20. echo htmlspecialchars($obj->strip($str));
  21. ?>


以上就是php 过滤html标记属性类的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!

人气教程排行