浅析php插件 HTMLPurifier HTML解析器

yipeiwu_com5年前PHP代码库
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);

相关文章

PHP实现简单爬虫的方法

本文实例讲述了PHP实现简单爬虫的方法。分享给大家供大家参考。具体如下: <?php /** * 爬虫程序 -- 原型 * * 从给定的url获取html内容...

关于php正则匹配汉字的方法介绍

php正则匹配汉字! /^[\x{4e00}-\x{9fa5}]+$/u以上这个正则表达式就是困扰了很多php程序员的匹配汉字的正则表达式大家可能会觉得很简单,实际上不同编码,不同程...

php删除文本文件中重复行的方法

本文实例讲述了php删除文本文件中重复行的方法。分享给大家供大家参考。具体分析如下: 这个php函数用来删除文件中的重复行,还可以指定是否忽略大小写,和指定换行符 /** * Re...

PHP 杂谈《重构-改善既有代码的设计》之三 重新组织数据

PHP 杂谈《重构-改善既有代码的设计》之三 重新组织数据

思维导图 介绍    承接上文的PHP 杂谈《重构-改善既有代码的设计》之 重新组织你的函数继续重构方面的内容。   这章主要针对数据的重构。 &nbs...

php去除重复字的实现代码

方法一: 复制代码 代码如下: $text = '数组aabbccdd'; $text_filter = ''; $filter = array(); $len = mb_strlen(...