Analysis on the HTMLPurifierHTML parser of the php plug-in

Source: Internet
Author: User
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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.