This article provides a detailed analysis of the php plug-in HTMLPurifierHTML parser. For more information, see the use of HTMLPurifier plug-in.
Download the HTMLPurifier plug-in
The useful part of the HTMLPurifier plug-in is library.
Use HTMLPurifier library class library
Method 1
The code is as follows:
Require_once 'htmlpurifier. auto. php ';
$ Config = HTMLPurifier_Config: createDefault ();
?>
Or
The code is as follows:
Require_once 'htmlpurifier. includes. php ';
Require_once 'htmlpurifier. autoload. php ';
$ Config = HTMLPurifier_Config: createDefault ();
?>
The example provided on the official website is
The code is as follows:
Require_once 'htmlpurifier. auto. php ';
My colleagues often use
The code is as follows:
Require_once 'htmlpurifier. includes. php ';
Require_once 'htmlpurifier. autoload. php ';
Set $ config
Configdoc
Http://htmlpurifier.org/live/configdoc/plain.html
Example
The code is as follows:
$ Config-> set ('HTML. allowedElements ', array ('P' => 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)
Elements allowed by HTML
P, table, tr, td, and br
New HTMLPurifier object
The code is as follows:
$ Purifier = new HTMLPurifier ($ config );
Call the purify method of the HTMLPurifier object
The code is as follows:
$ Puri_html = $ purifier-> purify ($ html );
Method 2
Customize a class HtmlPurifier. php
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:
The code is as follows:
$ Conf = array (
Array ('HTML. allowedelements ',
Array (
'P' => true,
'Table' => true,
'Tr' => true,
'TD '=> true,
'Br '=> true,
),
False), // allow the attribute p table tr td br element
Array ('HTML. AllowedAttributes ', array ('class' => TRUE), false), // allow attribute class
Array ('attr. ForbiddenClasses ', array ('resume _ p' => TRUE), false), // disable classes such
Array ('autoformat. removeempty', true, false), // Remove space
Array ('autoformat. RemoveEmpty. RemoveNbsp ', true, false), // go to nbsp
Array ('uri. disable', true, false ),
);
Call
The code is as follows:
$ P = new Resume_HtmlPurifier ($ conf );
$ Puri_html = $ p-> filter ($ html );