A brief analysis of the PHP plug-in HTMLPurifier HTML parser_PHP tutorial

WBOY
Release: 2016-07-21 15:01:56
Original
978 people have browsed it

How to use the HTMLPurifier plug-in
Download the HTMLPurifier plug-in
The useful part of the HTMLPurifier plug-in is the library


Use HTMLPurifier library
The first way

Copy code The code is as follows:

require_once 'HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
?>

or
Copy code The code is as follows:

< ?php
require_once 'HTMLPurifier.includes.php';
require_once 'HTMLPurifier.autoload.php';
$config = HTMLPurifier_Config::createDefault();
?>

The example given on the official website is
Copy code The code is as follows:

require_once ' HTMLPurifier.auto.php';

My colleagues commonly use
Copy code The code is as follows:

require_once 'HTMLPurifier.includes.php';
require_once 'HTMLPurifier.autoload.php'; >configdoc
http://htmlpurifier.org/live/configdoc/plain.html

Example


Copy code
The code is as follows:
$config->set('HTML.AllowedElements', array('div'=>true, 'table'=>true, 'tr'=>true , 'td'=>true, 'br'=>true));$config->set('HTML.Doctype', 'XHTML 1.0 Transitional') //html document type (permanent)$config->set('Core.Encoding', 'UTF-8') //Character encoding (permanent)

HTML allowed elements

div element, table element, tr element, td element, br element
new HTMLPurifier object


Copy code

The code is as follows:
$purifier = new HTMLPurifier($config);Call the purify method of HTMLPurifier object


Copy code

The code is as follows:
$puri_html = $purifier->purify($html);
The second way

Customize a class HtmlPurifier.php

Copy code
The code is as follows:
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);

}
}
?>


Set config information

For example:



Copy code
The code is as follows:

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/327964.htmlTechArticleHTMLPurifier插件的使用 下载HTMLPurifier插件 HTMLPurifier插件有用的部分是 library 使用HTMLPurifier library类库 第一种方式 复制代码 代码如下: ?php re...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!