HtmlAttributeFilter.class.php
<?php/** HTML Attribute Filter * date:2013-09-22 * author:fdipzone * ver:1.0 * * Func: * PU Blic Strip Filtering Properties * Public Setallow setting allowed properties * Public setexception setting Special case * Public SE Tignore Settings Ignored tags * Private findelements search for elements to be processed * Private findattributes Search Properties * Private Remov Eattributes Remove attribute * Private Isexception judge whether special case * Private Createattributes Create attribute * Private protect Special Character Escape */class htmlattributefilter{/class start private $_str = '; SOURCE string Private $_allow = Array (); Properties that allow retention such as array (' ID ', ' class ', ' title ') Private $_exception = Array (); Special cases such as: Array (' A ' =>array (' href ', ' class '), ' span ' =>array (' class ')) Private $_ignore = Array (); Ignore filtered tags For example: Array (' span ', ' img ')/** processing HTML, filtering attributes that are not preserved * @param string $str source String * @return S Tring */Public Function strip ($STR) {
$this->_str = $str; if (is_string ($this->_str) && strlen ($this->_str) >0) {//judgment string $this->_str = Strto Lower ($this->_str);
Convert to lowercase $res = $this->findelements ();
if (is_string ($res)) {return $res;
$nodes = $this->findattributes ($res);
$this->removeattributes ($nodes);
return $this->_str;
/** set allowed properties * @param Array $param/Public Function Setallow ($param =array ()) {
$this->_allow = $param;
/** Set Special case * @param Array $param/Public Function setexception ($param =array ()) {
$this->_exception = $param;
/** set ignored tag * @param Array $param/Public Function Setignore ($param =array ()) {
$this->_ignore = $param;
} /** search for elements to be processed/private function findelements () {$nodes = array ();
Preg_match_all ("/<" ([^!\/\>\n]+) ([^>]*) >/i ", $this->_str, $elements); foreach ($elements [1] as $el _key => $element) {if ($elements [2][$el _key]) {$literal = $el
ements[0][$el _key];
$element _name = $elements [1][$el _key];
$attributes = $elements [2][$el _key]; if (Is_array ($this->_ignore) &&!in_array ($element _name, $this->_ignore)) {$nodes [] = a
Rray (' literal ' => $literal, ' name ' => $element _name, ' attributes ' => $attributes);
}} if (! $nodes [0]) {return $this->_str;
}else{return $nodes; }/** Search Properties * @param Array $nodes elements to be handled/private function findattributes ($no DES) {foreach ($nodes as & $node) {Preg_match_all ("/([^ =]+) \s*=\s*[\" |] {0,1} ([^\"']*) [\"|']
{0,1}/i ", $node [' Attributes '], $attributes); if ($attributes [1]) {foreach ($attributes [1] as $att _key=> $att) {$literal = $attr
ibutes[0][$att _key];
$attribute _name = $attributes [1][$att _key];
$value = $attributes [2][$att _key];
$atts [] = Array (' literal ' => $literal, ' name ' => $attribute _name, ' value ' => $value);
}}else{$node [' attributes '] = NULL;
$node [' attributes '] = $atts;
Unset ($atts);
return $nodes; /** Remove attributes * @param Array $nodes elements to be processed * View this column more highlights: http://www.bianceng.cnhttp://www.biance ng.cn/webkf/php/*/Private Function removeattributes ($nodes) {foreach ($nodes as $node) {$ Node_name = $node [' NAMe '];
$new _attributes = '; if (Is_array ($node [' attributes '])) {foreach ($node [' attributes '] as $attribute) {if ( (Is_array ($this->_allow) && in_array ($attribute [' name '], $this->_allow)) | | $this->isexception ($node _name, $attribute [' name '], $this->_exception)) {$new _attributes =
$this->createattributes ($new _attributes, $attribute [' name '], $attribute [' value ']); }} $replacement = ($new _attributes)?
"< $node _name $new _attributes>": "< $node _name>"; $this->_str = preg_replace ('/'. $this->protect ($node [' literal ']). '
/', $replacement, $this->_str);
}/** to determine if a special case * @param string $element _name element name * @param string $attribute _name property name * @param Array $exceptions Permitted Exceptions * @return Boolean/Private Function IsexcePtion ($element _name, $attribute _name, $exceptions) {if (Array_key_exists ($element _name, $this->_exception)) {
if (In_array ($attribute _name, $this->_exception[$element _name]) {return true;
return false; /** Create Property * @param string $new _attributes * @param string $name * @param string $va Lue * @return String */Private Function createattributes ($new _attributes, $name, $value) {if ($new
_attributes) {$new _attributes. = "";
$new _attributes. = "$name =\" $value \ "";
return $new _attributes; /** Special Character Escape * @param string $str Source String * @return string/Private Function Protec T ($str) {$conversions = array ("^" => "\^", "[" => "\[", "." = > "\.", "$" => "\$", "{=>" \{"," * "=>" \* "," ("=>" \ "," \ "=>" \\\\ ","/"=&G T "\/", "+" => "\+", ")" => "\)", "|" => "\|", "?" => ;
"\?", "<" => "\<", ">" => "\>");
Return Strtr ($str, $conversions); }//Class end?>