直接贴代码,能够更好的说明使用:
1,把下载包放到third_party中。
2,在library中创建适合ci使用的类库,代码如下:
<?php
require APPPATH.'third_party/htmlpurifier/HTMLPurifier.auto.php';//引入放到third_party中的类库文件
class Htmlfilter extends HTMLPurifier{
protected $_htmlPurifier = null;
public function __construct($options = null){
$config = HTMLPurifier_Config::createDefault();
$config->set('Core.Encoding', 'UTF-8');
$config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
if(!empty($options)){
foreach($options as $option){
$config->set($option[0], $option[1]);
}
}
$this->_htmlPurifier = new HTMLPurifier($config);
}
public function filter($value){
return $this->_htmlPurifier->purify($value);
}
}
?>
3,在控制器中的使用,代码如下:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function test(){
$conf = array(
array('HTML.AllowedElements',
array(
'div' => true,
'table' => true,
'tr' => true,
'td' => true,
'br' => true,
'p' => true,
)), //允许属性 div table tr td br p元素
array('HTML.AllowedAttributes', array('class' => TRUE,'style'=>TRUE)),//允许属性 class 及style
array('Attr.ForbiddenClasses', array('resume_p' => TRUE)),//禁止classes如
array('AutoFormat.RemoveEmpty', true),//去空格
array('AutoFormat.RemoveEmpty.RemoveNbsp', true),//去nbsp
array('URI.Disable', true),
);
$this->load->library('Htmlfilter',$conf);
$html="<p style='color:red;'>hello world 1+1<3</p><script>alert(123)</script><style>color:red;</style>";
$html = $this->htmlfilter->filter($html);
echo $html;
}
}