-
-
/**
- @ 設定與保留屬性的類別
- @ class cleanHtml
- @ link:bbs.it-home.org
- @ date:2013/2/28
- */
- function reg_escape( $str )
- {
- $conversions = array ( $str )
- {
- $conversions = array ( $str )
- {
- $conversions = array ( $str )
- {
- $conversions = array ( $str )
- {
- $conversions = array ( $str )
- {
- $conversions = array ( $str 是"^" => "^", "[" => "[", "." => ".", "$" => "$", "{" => "{", "*" => “*”,“(”=>“(”,“\”=>“\\”,“/”=>“/”,“+”=>“+”,“)”=>“)”, “|”=>“|”,“?”=>“?”,““”=>;“>”);
- return strtr( $str, $conversions );
- }
-
- /**
- * 剝離屬性類別
- * 從 XML 元素中刪除屬性
- * @author David (semlabs.co.uk)
- * @version 0.2.1
- */
-
- class cleanHtml{
- public $str = '';
- public $allow = array();
- public $exceptions = array();
- public $ignore = array();
-
- public function strip( $str )
- {
- $this->str = $str;
-
- if( is_string( $str ) && strlen( $str ) > 0 )
- {
- $res = $this->findElements();
- if( is_string( $res ) )
- return $res;
- $nodes = $this->findAttributes( $res );
- $this->removeAttributes( $nodes );
- }
-
- return $this->str;
- }
-
- 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 = $elements [ 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[] = array( 'literal' => $literal, 'name ' => $element_name, '屬性' => $attributes );
- }
- }
-
- # 如果沒有要刪除的屬性,則傳回 XML
- if( !$nodes[0] )
- return $this->str;
- else
- 回傳 $nodes;
- }
-
- private function findAttributes( $nodes )
- {
-
- # 提取屬性
- foreach( $nodes as &$node )
- ( "/([^ =]+)s*=s*["|']{0,1}([^"']*)["|']{0,1}/i", $node[ '屬性'], $attributes );
- if( $attributes[1] )
- {
- foreach( $attributes[1] as $att_key => $att )
- {
- $文字= $attributes[0][$att_key]
- $attribute_name = $attributes[1][$att_key]
- $value = $attributes[2][$att_key]
- $atts [] = array ( 'literal' => $literal, '名稱' => $attribute_name, 'value' => $value );
- }
- } else $node ['屬性'] = null $node['屬性'] = $atts; 取消設定( $atts ); } 回傳$nodes }
-
- 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) ($thisy( $this) ->allow ) && in_array( $attribute['name'], $this->allow ) ) || $this->isException( $node_name, $attribute['name'], $this->例外) )
- $new_attributes = $this->createAttributes( $new_attributes, $attribute['name'], $attribute['value'] );
- }
- }
- $replacement = ( $new_attributes ) ? “”:"";
- $this->str = preg_replace( '/'.reg_escape( $node['literal'] ) .'/', $replacement, $this->str );
- }
- }
-
- 私有函式 isException( $element_name, $attribute_name, $exceptions )
- {
- if( array_key_exists($elelementname, $-Pel> > {
- if( in_array( $attribute_name, $this->exceptions[$element_name] ) )
- return true;
- }
-
- 回傳 false;
- }
-
- private function createAttributes( $new_attributes, $name, $value )
- {
- { $new_attributes .= " ";
- $new_attributes .= "$name="$value"";
-
- 回傳 $new_attributes;
- }
} ?>
複製程式碼
呼叫實例:
-
-
-
-
$str = '這裡是一些例子 html,其 被打破了 > ';
-
- $sa = 新的 cleanHtml;
- $sa->allow = array( 'id' );
-
- $sa->例外 = array(
- 'img' => array( 'src', 'alt' ),
- 'a' => array( 'href', '標題' ),
- 'iframe'=>array('src','frameborder'),
- );
-
echo $str = $sa->strip( $str ); ?>
複製程式碼 |