Analysis of PHP plugin htmlpurifier HTML parser

Source: Internet
Author: User
Tags autoload
This article is the PHP plug-in htmlpurifier HTML parser for a detailed analysis of the introduction, the need for friends to refer to the

Use of Htmlpurifier Plug-ins
Download Htmlpurifier Plugin
The useful part of the Htmlpurifier plugin is the library


using Htmlpurifier Library class libraries
The first Way

Copy Code code as follows:


<?php


require_once ' HTMLPurifier.auto.php ';


$config = Htmlpurifier_config::createdefault ();


?>


or

Copy Code code as follows:


<?php


require_once ' HTMLPurifier.includes.php ';


require_once ' HTMLPurifier.autoload.php ';


$config = Htmlpurifier_config::createdefault ();


?>


An example from the official website is

Copy Code code as follows:


require_once ' HTMLPurifier.auto.php ';


what my colleagues used to do

Copy Code code as follows:


require_once ' HTMLPurifier.includes.php ';


require_once ' HTMLPurifier.autoload.php ';


set $config
Configdoc
http://htmlpurifier.org/live/configdoc/plain.html
Example

Copy Code code 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 elements, table elements, TR elements, TD elements, BR elements
New Htmlpurifier Object

Copy Code code as follows:


$purifier = new Htmlpurifier ($config);


To invoke the purify method of the Htmlpurifier object

Copy Code code as follows:


$puri _html = $purifier->purify ($html);


The second way
Customizing a class htmlpurifier.php

Copy Code code 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 Code code as follows:


$conf = Array (


Array (' HTML. Allowedelements ',


Array (


' div ' => true,


' table ' => true,


' tr ' => true,


' TD ' => True,


' BR ' => true,


),


false),//Allow Property div table tr td br Element


Array (' HTML. Allowedattributes ', Array (' class ' => TRUE), false),//Allow property class


Array (' attr.forbiddenclasses ', Array (' resume_p ' => TRUE), false),//Prohibit classes such as


Array (' Autoformat.removeempty ', true, false),//go to space


Array (' AutoFormat.RemoveEmpty.RemoveNbsp ', true, false),//go to nbsp


Array (' URI. Disable ', True, false],


);


Call

Copy Code code 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.