首頁  >  文章  >  後端開發  >  php寫的設定過濾及保留屬性的類

php寫的設定過濾及保留屬性的類

WBOY
WBOY原創
2016-07-25 09:04:191007瀏覽
  1. /**
  2. @ 設定與保留屬性的類別
  3. @ class cleanHtml
  4. @ link:bbs.it-home.org
  5. @ date:2013/2/28
  6. */
  7. function reg_escape( $str )
  8. {
  9. $conversions = array ( $str )
  10. {
  11. $conversions = array ( $str )
  12. {
  13. $conversions = array ( $str )
  14. {
  15. $conversions = array ( $str )
  16. {
  17. $conversions = array ( $str )
  18. {
  19. $conversions = array ( $str 是"^" => "^", "[" => "[", "." => ".", "$" => "$", "{" => "{", "*" => “*”,“(”=>“(”,“\”=>“\\”,“/”=>“/”,“+”=>“+”,“)”=>“)”, “|”=>“|”,“?”=>“?”,““”=>;“>”);
  20. return strtr( $str, $conversions );
  21. }
  22. /**
  23. * 剝離屬性類別
  24. * 從 XML 元素中刪除屬性
  25. * @author David (semlabs.co.uk)
  26. * @version 0.2.1
  27. */
  28. class cleanHtml{
  29. public $str = '';
  30. public $allow = array();
  31. public $exceptions = array();
  32. public $ignore = array();
  33. public function strip( $str )
  34. {
  35. $this->str = $str;
  36. if( is_string( $str ) && strlen( $str ) > 0 )
  37. {
  38. $res = $this->findElements();
  39. if( is_string( $res ) )
  40. return $res;
  41. $nodes = $this->findAttributes( $res );
  42. $this->removeAttributes( $nodes );
  43. }
  44. return $this->str;
  45. }
  46. private function findElements()
  47. {
  48. # 建立具有屬性的元素陣列
  49. $nodes = array();
  50. preg_match_all( "/n]+)([^>]*)>/i", $this->str, $elements );
  51. foreach( $elements[1] as $el_key => $element )
  52. {
  53. if( $elements[2][$el_key] )
  54. {
  55. $literal = $elements [ 0][$el_key];
  56. $element_name = $elements[1][$el_key];
  57. $attributes = $elements[2][$el_key];
  58. if( is_array( $this->ignore ) && !in_array( $element_name, $this->ignore ) )
  59. $nodes[] = array( 'literal' => $literal, 'name ' => $element_name, '屬性' => $attributes );
  60. }
  61. }
  62. # 如果沒有要刪除的屬性,則傳回 XML
  63. if( !$nodes[0] )
  64. return $this->str;
  65. else
  66. 回傳 $nodes;
  67. }
  68. private function findAttributes( $nodes )
  69. {
  70. # 提取屬性
  71. foreach( $nodes as &$node )
  72. ( "/([^ =]+)s*=s*["|']{0,1}([^"']*)["|']{0,1}/i", $node[ '屬性'], $attributes );
  73. if( $attributes[1] )
  74. {
  75. foreach( $attributes[1] as $att_key => $att )
  76. {
  77. $文字= $attributes[0][$att_key]
  78. $attribute_name = $attributes[1][$att_key]
  79. $value = $attributes[2][$att_key]
  80. $atts [] = array ( 'literal' => $literal, '名稱' => $attribute_name, 'value' => $value );
  81. }
  82. } else $node ['屬性'] = null $node['屬性'] = $atts; 取消設定( $atts ); } 回傳$nodes }
  83. private function removeAttributes( $nodes )
  84. {
  85. # 不需要刪除的屬性
  86. foreach( $nodes as $node )
  87. {
  88. {
  89. {
  90. # 檢查如果節點有任何屬性要保留
  91. $node_name = $node['name'];
  92. $new_attributes = '';
  93. if( is_array( $node['attributes'] ) )
  94. {
  95. foreach( $node['attributes'] as $attribute )
  96. {
  97. if( ( is_array( $this) ($thisy( $this) ->allow ) && in_array( $attribute['name'], $this->allow ) ) || $this->isException( $node_name, $attribute['name'], $this->例外) )
  98. $new_attributes = $this->createAttributes( $new_attributes, $attribute['name'], $attribute['value'] );
  99. }
  100. }
  101. $replacement = ( $new_attributes ) ? “”:"";
  102. $this->str = preg_replace( '/'.reg_escape( $node['literal'] ) .'/', $replacement, $this->str );
  103. }
  104. }
  105. 私有函式 isException( $element_name, $attribute_name, $exceptions )
  106. {
  107. if( array_key_exists($elelementname, $-Pel> > {
  108. if( in_array( $attribute_name, $this->exceptions[$element_name] ) )
  109. return true;
  110. }
  111. 回傳 false;
  112. }
  113. private function createAttributes( $new_attributes, $name, $value )
  114. {
  115. { $new_attributes .= " ";
  116. $new_attributes .= "$name="$value"";
  117. 回傳 $new_attributes;
  118. }
}
?>
複製程式碼

呼叫實例:
  1. $str = '這裡是一些例子 html,其 被打破了 php寫的設定過濾及保留屬性的類> ';
  2. $sa = 新的 cleanHtml;
  3. $sa->allow = array( 'id' );
  4. $sa->例外 = array(
  5. 'img' => array( 'src', 'alt' ),
  6. 'a' => array( 'href', '標題' ),
  7. 'iframe'=>array('src','frameborder'),
  8. );
echo $str = $sa->strip( $str );
?>
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn