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

如何通过php过滤html标记属性类

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

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'], $this->_exception)){
  110. $new_attributes = $this->createAttributes($new_attributes, $attribute['name'], $attribute['value']);
  111. }
  112. }
  113. }
  114. $replacement = ($new_attributes) ? "<$node_name $new_attributes>" : "<$node_name>";
  115. $this->_str = preg_replace('/'.$this->protect($node['literal']).'/', $replacement, $this->_str);
  116. }
  117. }
  118. /** 判断是否特例
  119. * @param String $element_name 元素名
  120. * @param String $attribute_name 属性名
  121. * @param Array $exceptions 允许的特例
  122. * @return boolean
  123. */
  124. private function isException($element_name, $attribute_name, $exceptions){
  125. if(array_key_exists($element_name, $this->_exception)){
  126. if(in_array($attribute_name, $this->_exception[$element_name])){
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132. /** 创建属性
  133. * @param String $new_attributes
  134. * @param String $name
  135. * @param String $value
  136. * @return String
  137. */
  138. private function createAttributes($new_attributes, $name, $value){
  139. if($new_attributes){
  140. $new_attributes .= " ";
  141. }
  142. $new_attributes .= "$name=\"$value\"";
  143. return $new_attributes;
  144. }
  145. /** 特殊字符转义
  146. * @param String $str 源字符串
  147. * @return String
  148. */
  149. private function protect($str){
  150. $conversions = array(
  151. "^" => "\^",
  152. "[" => "\[",
  153. "." => "\.",
  154. "$" => "\$",
  155. "{" => "\{",
  156. "*" => "\*",
  157. "(" => "\(",
  158. "\\" => "\\\\",
  159. "/" => "\/",
  160. "+" => "\+",
  161. ")" => "\)",
  162. "|" => "\|",
  163. "?" => "\?",
  164. "<" => "\<",
  165. ">" => "\>"
  166. );
  167. return strtr($str, $conversions);
  168. }
  169. } // class end
  170. ?>

demo

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

本文讲解了如何通过php 过滤html标记属性类 ,更多先关内容请关注Gxl网。

相关推荐:

如何使用php 替换敏感字符串的相关操作

关于PHP 遍历文件夹及文件类及处理类

如何通过php 获取页面中的指定内容类

以上就是如何通过php 过滤html标记属性类的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行