php设置类,php写的设置过滤及保留属性的类

php写的设置过滤及保留属性的类,有需要的朋友可以研究下。

复制代码 代码如下:

/**

@ 设置及保留属性的类

@ class cleanHtml

@ link:www.jquerycn.cn

@ date:2013/2/28

*/

function reg_escape( $str )

{

$conversions = array( "^" => "\^", "[" => "\[", "." => "\.", "$" => "\$", "{" => "\{", "*" => "\*", "(" => "\(", "\\" => "\\\\", "/" => "\/", "+" => "\+", ")" => "\)", "|" => "\|", "?" => "\?", " "\" => "\>" );

return strtr( $str, $conversions );

}

/**

* Strip attribute Class

* Remove attributes from XML elements

* @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()

{

# Create an array of elements with attributes

$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' => $attributes );

}

}

# Return the XML if there were no attributes to remove

if( !$nodes[0] )

return $this->str;

else

return $nodes;

}

private function findAttributes( $nodes )

{

# Extract attributes

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 = $attributes[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;

}

private function removeAttributes( $nodes )

{

# Remove unwanted attributes

foreach( $nodes as $node )

{

# Check if node has any attributes to be kept

$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->exceptions ) )

$new_attributes = $this->createAttributes( $new_attributes, $attribute['name'], $attribute['value'] );

}

}

$replacement = ( $new_attributes ) ? "" : "";

$this->str = preg_replace( '/'. reg_escape( $node['literal'] ) .'/', $replacement, $this->str );

}

}

private function isException( $element_name, $attribute_name, $exceptions )

{

if( array_key_exists($element_name, $this->exceptions) )

{

if( in_array( $attribute_name, $this->exceptions[$element_name] ) )

return true;

}

return false;

}

private function createAttributes( $new_attributes, $name, $value )

{

if( $new_attributes )

$new_attributes .= " ";

$new_attributes .= "$name=\"$value\"";

return $new_attributes;

}

}

?>

调用实例:

复制代码 代码如下:

$str = 'Here is some sample html that is getting broken featuredon.jpg'; 

$sa = new cleanHtml;

$sa->allow = array( 'id' );

$sa->exceptions = array(

'img' => array( 'src', 'alt' ),

'a' => array( 'href', 'title' ),

'iframe'=>array('src','frameborder'),

);

echo $str = $sa->strip( $str );

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值