Analysis on the HTML Parser of the php plug-in HTMLPurifier

Source: Internet
Author: User
Tags autoload

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

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.