浅析php插件 HTMLPurifier HTML解析器
2015-01-24信息快讯网
本篇文章是对php插件 HTMLPurifier HTML解析器进行了详细的分析介绍,需要的朋友参考下
HTMLPurifier插件的使用下载HTMLPurifier插件
HTMLPurifier插件有用的部分是 library
使用HTMLPurifier library类库
第一种方式
<?php require_once 'HTMLPurifier.auto.php'; $config = HTMLPurifier_Config::createDefault(); ?>
或者
<?php require_once 'HTMLPurifier.includes.php'; require_once 'HTMLPurifier.autoload.php'; $config = HTMLPurifier_Config::createDefault(); ?>
官网给出的例子是
require_once 'HTMLPurifier.auto.php';
我同事常用的是
require_once 'HTMLPurifier.includes.php'; require_once 'HTMLPurifier.autoload.php';
设置$config
configdoc
http://htmlpurifier.org/live/configdoc/plain.html
例子
$config->set('HTML.AllowedElements', array('div'=>true, 'table'=>true, 'tr'=>true, 'td'=>true, 'br'=>true));
$config->set('HTML.Doctype', 'XHTML 1.0 Transitional') //html文档类型(常设)
$config->set('Core.Encoding', 'UTF-8') //字符编码(常设)
HTML允许的元素
div元素,table元素,tr元素,td元素,br元素
new HTMLPurifier对象
$purifier = new HTMLPurifier($config);
调用HTMLPurifier对象的purify方法
$puri_html = $purifier->purify($html);
第二种方式
自定义一个类 HtmlPurifier.php
<?php
require_once 'HTMLPurifier.includes.php';
require_once 'HTMLPurifier.autoload.php';
class Resume_HtmlPurifier implements Zend_Filter_Interface{
protected $_htmlPurifier = null;
public function __construct($options = null)
{
$config = HTMLPurifier_Config::createDefault();
$config->set('Code.Encoding', 'UTF-8');
$config->set('HTML.Doctype', 'XHTML 1.0 Transitional')
if(!is_null($options)){
foreach($options as $option){
$config->set($option[0], $option[1], $option[2]);
}
}
$this->_htmlPurifier = new HTMLPurifier($config);
}
public function filter($value)
{
return $this->_htmlPurifier->purify($value);
}
}
?>
设置config信息
例如:
$conf = array(
array('HTML.AllowedElements',
array(
'div' => true,
'table' => true,
'tr' => true,
'td' => true,
'br' => true,
),
false), //允许属性 div table tr td br元素
array('HTML.AllowedAttributes', array('class' => TRUE), false), //允许属性 class
array('Attr.ForbiddenClasses', array('resume_p' => TRUE), false), //禁止classes如
array('AutoFormat.RemoveEmpty', true, false), //去空格
array('AutoFormat.RemoveEmpty.RemoveNbsp', true, false), //去nbsp
array('URI.Disable', true, false),
);
调用
$p = new Resume_HtmlPurifier($conf); $puri_html = $p->filter($html);
xss防御之php利用httponly防xss攻击
php获取表单中多个同名input元素的值
PHP fopen()和 file_get_contents()应用与差异介绍
PHP中CURL的CURLOPT_POSTFIELDS参数使用细节
zf框架的Filter过滤器使用示例
PHP中is_file不能替代file_exists的理由
php中调用其他系统http接口的方法说明
将php数组输出html表格的方法
php检测iis环境是否支持htaccess的方法
jQuery中的RadioButton,input,CheckBox取值赋值实现代码
PHP strip_tags()去除HTML、XML以及PHP的标签介绍
php 判断字符串中是否包含html标签
PHP中$_FILES的使用方法及注意事项说明
php获取网页标题和内容函数(不包含html标签)
php缓冲 output_buffering和ob_start使用介绍
PHP 动态生成静态HTML页面示例代码
php输入流php://input使用示例(php发送图片流到服务器)
php安装xdebug/php安装pear/phpunit详解步骤(图)
解析php根据ip查询所在地区(非常有用,赶集网就用到)
浅析php插件 Simple HTML DOM 用DOM方式处理HTML
基于simple_html_dom的使用小结
file_get_contents("php://input", "r")实例介绍
PHP删除HTMl标签的三种解决方法
PHP删除HTMl标签的实现代码
浅析php面向对象public private protected 访问修饰符
浅析HTTP消息头网页缓存控制以及header常用指令介绍
解析file_get_contents模仿浏览器头(user_agent)获取数据
php 批量生成html,txt文件的实现代码
解析php file_exists无效的解决办法
深入file_get_contents与curl函数的详解
执行、获取远程代码返回:file_get_contents 超时处理的问题详解
php常用Output和ptions/Info函数集介绍
php class中public,private,protected的区别以及实例分析
基于PHP输出缓存(output_buffering)的深入理解