PHPフィルタHTMLタグ属性クラス(ソースコード添付)

WBOY
リリース: 2016-07-25 08:55:20
オリジナル
902 人が閲覧しました
  1. /**HTML 属性フィルター

  2. * 日付: 2013-09-22
  3. * 著者: fdipzone
  4. * バージョン: 1.0
  5. * 編集: bbs.it-home.org
  6. * 機能:
  7. * パブリック ストリップ フィルター属性
  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 protected 特殊文字エスケープ
  17. */
  18. class HtmlAttributeFilter{ // クラス開始
  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 ストリップ($str){
  28. $this->_str = $str;
  29. if(is_string($this->_str) && strlen($this -> ;_str)>0){ // 文字列を決定します
  30. $this->_str = strto lower($this->_str) // 小文字に変換します
  31. $res = $this->findElements; () ;
  32. if(is_string($res)){
  33. $res; }
  34. $this->removeAttributes($nodes);
  35. return $this->_str;
  36. }
  37. /**許可されるプロパティを設定します
  38. * @param Array $param
  39. */
  40. public function setAllow($param=array()){
  41. $this->_allow = $param }
  42. / ** 特別なケースを設定します
  43. * @param Array $param
  44. */
  45. public function setException($param=array()){
  46. $this->Exception = $param; }
  47. /**無視されるタグを設定します
  48. * @param Array $param
  49. */
  50. public function setIgnore($param) =array ()){
  51. $this->_ignore = $param; }
  52. /**処理対象の要素を検索する*/
  53. プライベート関数 findElements(){
  54. $nodes = array();
  55. preg_match_all("/< ;( [^ !/>n]+)([^>]*)>/i", $this->_str, $elements);
  56. foreach($elements[1] as $el_key => ; $要素){
  57. if($elements[2][$el_key]){
  58. $literal = $elements[0][$el_key];
  59. $attributes = $ elements[2][$el_key];
  60. if(is_array($this->_ignore) && !in_array($element_name, $this->_ignore)){
  61. $nodes[] = array('literal' => ;$literal, 'name'=>$element_name, 'attributes'=>$attributes);
  62. }
  63. }
  64. }
  65. if(!$nodes[0]){
  66. return $this-> _str;
  67. }else{
  68. return $nodes;
  69. }
  70. }
  71. /**検索属性
  72. * @param 処理対象の配列 $nodes 要素
  73. */
  74. プライベート関数 findAttributes($nodes){
  75. foreach($nodes as &$node){
  76. preg_match_all(" /( [^ =]+)s*=s*["|']{0,1}([^"']*)["|']{0,1}/i", $node['attributes '] , $attributes);
  77. if($attributes[1]){
  78. foreach($attributes[1] as $att_key=>$att){
  79. $literal = $attributes[0][$att_key]; $attribute_name = $attributes[1][$att_key];
  80. $value = $attributes[2][$att_key]
  81. $atts[] = array('literal'=>$literal, 'name'=> $attribute_name , 'value'=>$value);
  82. }
  83. }else{
  84. $node['attributes'] = null;
  85. $node['attributes'] = $atts; ;
  86. }
  87. $nodes を返します
  88. }
  89. /**属性を削除します
  90. * @param 処理される配列 $nodes 要素
  91. */
  92. プライベート関数 RemoveAttributes($nodes){
  93. foreach($nodes as $node){
  94. $node_name = $node['name'];
  95. $new_attributes = '';
  96. if(is_array($node['attributes'])){
  97. foreach($node['attributes'] as $attribute){
  98. if((is_array($this->_allow) && in_array($attribute[' name'], $this->_allow)) || $this->isException($node_name, $attribute['name'], $this->_Exception)){
  99. $new_attributes = $this-> createAttributes($new_attributes, $attribute['name'], $attribute['value']);
  100. }
  101. }
  102. }
  103. $replacement = ($new_attributes) ? "<$node_name $new_attributes>" : "<$node_name>";
  104. $this->_str = preg_replace('/'.$this->protect($node['literal']).'/', $replacement, $this->_str);
  105. }
  106. }
  107. /**特殊なケースかどうかを判断します
  108. * @param String $element_name 要素名
  109. * @param String $attribute_name 属性名
  110. * @param Array $Exceptions は特殊なケースを許可します
  111. * @return boolean
  112. */
  113. プライベート関数 isException($element_name, $attribute_name, $Exceptions){
  114. if(array_key_exists($element_name, $this->_Exception)){
  115. if(in_array ($attribute_name, $this->_Exception[$element_name])){
  116. true を返します。
  117. }
  118. }
  119. false を返します。
  120. }

  121. /**プロパティ

  122. * @param String $new_attributes
  123. * @param String $name
  124. * @param String $value
  125. * @return String
  126. */
  127. プライベート関数 createAttributes($new_attributes, $name, $value){
  128. if($new_attributes){
  129. $new_attributes .= " ";
  130. }
  131. $new_attributes .= "$name="$value"";
  132. $new_attributes を返します;
  133. }
  134. /**特殊文字エスケープ
  135. * @param String $str ソース文字列
  136. * @return String
  137. */
  138. private function protected($str){
  139. $conversions = array(
  140. "^" => "^",
  141. "[" => "[",
  142. "." => "."、
  143. "$" => "{"、
  144. "(" => "("、
  145. "\" => "\\"、
  146. "/" => "/"、
  147. "+" => "+"、
  148. ")" => ")"、
  149. "|" => "|"、
  150. "<"、
  151. ">"
  152. return strtr($str, $conversions);
  153. }
  154. } // 授業終了
  155. ?>

  156. 复制代

2、デモ例

  1. require('HtmlAttributeFilter.class.php');
  2. $str = '';
  3. $obj = new HtmlAttributeFilter()// ID 属性を許可
  4. $ obj->setAllow(array('id'));
  5. $obj->setException(array(
  6. 'a' => array('href'), // タグは、href 属性の特殊なケースを許可します
  7. ' ul' => array('class') // ul タグはクラス属性の特殊なケースを許可します
  8. // img タグは無視され、属性はフィルタされません
  9. $obj->setIgnore(array) ('img') );
  10. echo 'ソース str:
    ';
  11. echo htmlspecialchars($str).'

    '; ;
  12. echo htmlspecialchars( $obj->strip($str));
  13. ?>
  14. PHPフィルタリングHTMLタグ属性クラスのソースコードダウンロードアドレスをコピーします

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!