PHP Filter HTML tag attribute class (with source)

Source: Internet
Author: User
  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 Filter Properties
  8. * Public Setallow setting allowed properties
  9. * Public SetException Set exceptions
  10. * Public Setignore Set ignored tags
  11. * Private findelements search for elements to be processed
  12. * Private Findattributes Search Properties
  13. * Private Removeattributes Removal properties
  14. * Private Isexception judge whether special
  15. * Private Createattributes Create attribute
  16. * Private protect special character escapes
  17. */
  18. Class htmlattributefilter{//Class start
  19. Private $_str = '; SOURCE string
  20. Private $_allow = Array (); Allowed attributes such as: Array (' ID ', ' class ', ' title ')
  21. Private $_exception = Array (); Exceptions For example: Array (' A ' =>array (' href ', ' class '), ' span ' =>array (' class '))
  22. Private $_ignore = Array (); Ignore filtered tags such as: Array (' span ', ' img ')
  23. /** processing HTML, filtering non-reserved properties
  24. * @param string $STR source strings
  25. * @return String
  26. */
  27. Public function strip ($STR) {
  28. $this->_str = $str;
  29. if (is_string ($this->_str) && strlen ($this->_str) >0) {//Judgment string
  30. $this->_str = strtolower ($this->_str); Turn into lowercase
  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. /** Setting the Allowed properties
  41. * @param Array $param
  42. */
  43. Public Function Setallow ($param =array ()) {
  44. $this->_allow = $param;
  45. }
  46. /** Setting exceptions
  47. * @param Array $param
  48. */
  49. Public Function SetException ($param =array ()) {
  50. $this->_exception = $param;
  51. }
  52. /** Setting ignored tags
  53. * @param Array $param
  54. */
  55. Public Function Setignore ($param =array ()) {
  56. $this->_ignore = $param;
  57. }
  58. /** search for elements to be processed */
  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. /** Search Properties
  79. * @param Array $nodes The element to be processed
  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. /** Removing properties
  100. * @param Array $nodes The element to be processed
  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)? "< $node _name $new _attributes>": "< $node _name>";
  114. $this->_str = preg_replace ('/'. $this->protect ($node [' literal ']). ' /', $replacement, $this->_str);
  115. }
  116. }
  117. /** judge whether the exception
  118. * @param String $element _name element name
  119. * @param String $attribute _name Property name
  120. * @param Array $exceptions allowed 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. /** Creating properties

  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. /** Special Character escapes
  145. * @param string $STR source strings
  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. );
  166. Return Strtr ($str, $conversions);
  167. }
  168. }//Class end
  169. ?>

Copy Code

2, demo example

    1. Require (' HtmlAttributeFilter.class.php ');
    2. $str = '
      • Yuna

        Love

        want to know YES
      ';
    3. $obj = new Htmlattributefilter ();
    4. Allow ID attribute
    5. $obj->setallow (Array (' ID '));
    6. $obj->setexception (Array (
    7. ' A ' + = Array (' href '),//a tag with href attribute exception allowed
    8. The ' ul ' = = Array (' class ')//UL tag allows a special case of class attribute
    9. ));
    10. IMG tag ignored, no attributes are filtered
    11. $obj->setignore (Array (' IMG '));
    12. echo ' Source str:
      ';
    13. echo Htmlspecialchars ($str). '

      ';
    14. Echo ' Filter str:
      ';
    15. Echo Htmlspecialchars ($obj->strip ($STR));
    16. ?>
Copy Code

Attached, PHP filter HTML tag attribute class source download address

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.