前端之家收集整理的这篇文章主要介绍了
Yii净化器CHtmlPurifier用法示例(过滤不良代码),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_0@本文实例讲述了Yii净化器CHtmlPurifier用法。分享给大家供大家参考,具体如下:
@H_
502_0@1. 在控制器中使用:
<div class="jb51code">
<pre class="brush:php;">
public function actionCreate()
{
$model=new News;
$purifier = new CHtmlPurifier();
$purifier->options = array(
'URI.AllowedSchemes'=>array(
'http' => true,'https' => true,),'HTML.Allowed'=>'div',);
if(isset($_POST['News']))
{
$model->attributes=$_POST['News'];
$model->attributes['content'] = $purifier->purify($model->attributes['content']);
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
}
_0@2. 在模型中的使用:
options = array(
'URI.AllowedSchemes'=>array(
'http' => true,);
if(parent::beforeSave()){
if($this->isNewRecord){
$this->create_data = date('y-m-d H:m:s');
$this->content = $purifier->purify($this->content);
}
return true;
}else{
return false;
}
}
_0@3. 在过滤器中的使用:
options = array(
'URI.AllowedSchemes'=>array(
'http' => true,);
if(isset($_POST['news']){
$_POST['news']['content'] = $purify($_POST['news']['content']);
}
$filterChain->run();
}
_0@4. 在视图中的使用:
PHP;">
beginWidget('CHtmlPurifier'); ?>
...display user-entered content here...
endWidget(); ?>