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
Copy codeThe Code is as follows: <? Php
Require_once 'htmlpurifier. auto. php ';
$ Config = HTMLPurifier_Config: createDefault ();
?>
Or
Copy codeThe Code is as follows: <? Php
Require_once 'htmlpurifier. includes. php ';
Require_once 'htmlpurifier. autoload. php ';
$ Config = HTMLPurifier_Config: createDefault ();
?>
The example provided on the official website is
Copy codeThe Code is as follows: require_once 'htmlpurifier. auto. php ';
My colleagues often use
Copy codeThe 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
Copy codeThe 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)
Elements allowed by HTML
Div element, table element, tr element, td element, br Element
New HTMLPurifier objectCopy codeThe Code is as follows: $ purifier = new HTMLPurifier ($ config );
Call the purify Method of the HTMLPurifier objectCopy codeThe Code is as follows: $ puri_html = $ purifier-> purify ($ html );
Method 2
Customize a class HtmlPurifier. phpCopy codeThe Code is as follows: <? 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 );
}
}
?>
Set config information
For example:
Copy codeThe Code is as follows: $ conf = array (
Array ('html. allowedelements ',
Array (
'Div '=> true,
'Table' => true,
'Tr' => true,
'Td '=> true,
'Br '=> true,
),
False), // allow the div 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
Copy codeThe Code is as follows: $ p = new Resume_HtmlPurifier ($ conf );
$ Puri_html = $ p-> filter ($ html );