php clean html 可以设置过滤及保留属性

来源:互联网 发布:哈德逊河迫降 知乎 编辑:程序博客网 时间:2024/06/07 17:31

类:

Php代码  收藏代码
  1. <?php  
  2.   
  3. function reg_escape( $str )  
  4. {  
  5.     $conversions = array"^" => "\^""[" => "\[""." => "\.""$" => "\$""{" => "\{""*" => "\*""(" => "\(""\\" => "\\\\", "/" => "\/", "+" => "\+", ")" => "\)", "|" => "\|", "?" => "\?", "<" => "\<", ">" => "\>" );  
  6.     return strtr$str$conversions );  
  7. }  
  8.   
  9. /** 
  10. * Strip attribute Class 
  11. * Remove attributes from XML elements 
  12. * @author David (semlabs.co.uk) 
  13. * @version 0.2.1 
  14. */  
  15.   
  16. class cleanHtml{  
  17.       
  18.     public $str         = '';  
  19.     public $allow       = array();  
  20.     public $exceptions  = array();  
  21.     public $ignore      = array();  
  22.       
  23.     public function strip( $str )  
  24.     {  
  25.         $this->str = $str;  
  26.           
  27.         ifis_string$str ) && strlen$str ) > 0 )  
  28.         {  
  29.             $res = $this->findElements();  
  30.             ifis_string$res ) )  
  31.                 return $res;  
  32.             $nodes = $this->findAttributes( $res );  
  33.             $this->removeAttributes( $nodes );  
  34.         }  
  35.           
  36.         return $this->str;  
  37.     }  
  38.       
  39.     private function findElements()  
  40.     {  
  41.           
  42.         # Create an array of elements with attributes  
  43.         $nodes = array();  
  44.         preg_match_all( "/<([^ !\/\>\n]+)([^>]*)>/i"$this->str, $elements );  
  45.         foreach$elements[1] as $el_key => $element )  
  46.         {  
  47.             if$elements[2][$el_key] )  
  48.             {  
  49.                 $literal = $elements[0][$el_key];  
  50.                 $element_name = $elements[1][$el_key];  
  51.                 $attributes = $elements[2][$el_key];  
  52.                 ifis_array$this->ignore ) && !in_array( $element_name$this->ignore ) )  
  53.                     $nodes[] = array'literal' => $literal'name' => $element_name'attributes' => $attributes );  
  54.             }  
  55.         }  
  56.           
  57.         # Return the XML if there were no attributes to remove  
  58.         if( !$nodes[0] )  
  59.             return $this->str;  
  60.         else  
  61.             return $nodes;  
  62.     }  
  63.       
  64.     private function findAttributes( $nodes )  
  65.     {  
  66.           
  67.         # Extract attributes  
  68.         foreach$nodes as &$node )  
  69.         {  
  70.             preg_match_all( "/([^ =]+)\s*=\s*[\"|']{0,1}([^\"']*)[\"|']{0,1}/i"$node['attributes'], $attributes );  
  71.             if$attributes[1] )  
  72.             {  
  73.                 foreach$attributes[1] as $att_key => $att )  
  74.                 {  
  75.                     $literal = $attributes[0][$att_key];  
  76.                     $attribute_name = $attributes[1][$att_key];  
  77.                     $value = $attributes[2][$att_key];  
  78.                     $atts[] = array'literal' => $literal'name' => $attribute_name'value' => $value );  
  79.                 }  
  80.             }  
  81.             else  
  82.                 $node['attributes'] = null;  
  83.               
  84.             $node['attributes'] = $atts;  
  85.             unset( $atts );  
  86.         }  
  87.           
  88.         return $nodes;  
  89.     }  
  90.       
  91.     private function removeAttributes( $nodes )  
  92.     {  
  93.           
  94.         # Remove unwanted attributes  
  95.         foreach$nodes as $node )  
  96.         {  
  97.               
  98.             # Check if node has any attributes to be kept  
  99.             $node_name = $node['name'];  
  100.             $new_attributes = '';  
  101.             ifis_array$node['attributes'] ) )  
  102.             {  
  103.                 foreach$node['attributes'as $attribute )  
  104.                 {  
  105.                     if( ( is_array$this->allow ) && in_array( $attribute['name'], $this->allow ) ) || $this->isException( $node_name$attribute['name'], $this->exceptions ) )  
  106.                         $new_attributes = $this->createAttributes( $new_attributes$attribute['name'], $attribute['value'] );  
  107.                 }  
  108.             }  
  109.             $replacement = ( $new_attributes ) ? "<$node_name $new_attributes>" : "<$node_name>";  
  110.             $this->str = preg_replace( '/'. reg_escape( $node['literal'] ) .'/'$replacement$this->str );  
  111.         }  
  112.           
  113.     }  
  114.       
  115.     private function isException( $element_name$attribute_name$exceptions )  
  116.     {  
  117.         ifarray_key_exists($element_name$this->exceptions) )  
  118.         {  
  119.             if( in_array( $attribute_name$this->exceptions[$element_name] ) )  
  120.                 return true;  
  121.         }  
  122.           
  123.         return false;  
  124.     }  
  125.       
  126.     private function createAttributes( $new_attributes$name$value )  
  127.     {  
  128.         if$new_attributes )  
  129.             $new_attributes .= " ";  
  130.         $new_attributes .= "$name=\"$value\"";  
  131.           
  132.         return $new_attributes;  
  133.     }  
  134.   
  135. }  
 

实例:

C代码  收藏代码
  1. $str = 'Here is some sample html that is <span id="good" class="aClass" style="abc" font="3"> <font color="red"> getting broken </font> </span> <iframe width="540" height="304" src="http://www.youtube.com/embed/YacZqlFz2bI?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe> <img id="featuredon" src="http://www.goodfinancialcents.com/wp-content/themes/thesis_182/custom/images/featuredon.jpg" height="23" width="265" />';  
  2.   
  3. $sa = new cleanHtml;  
  4. $sa->allow = array( 'id' );    
  5.   
  6.  $sa->exceptions = array(  
  7.         'img' => array( 'src''alt' ),  
  8.         'a' => array( 'href''title' ),  
  9.         'iframe'=>array('src','frameborder'),  
  10.         );  
  11.   
  12. echo $str = $sa->strip( $str );