Home PHP Libraries Other libraries htmlpurifierPHP filtering library
htmlpurifierPHP filtering library

require_once '/path/to/HTMLPurifier.auto.php';

According to ThinkPHP specifications, for third-party extensions that do not comply with ThinkPHP development specifications, HTMLPurifier needs to be placed in the Library/Vendor directory middle. Then we can introduce HTMLPurifier.auto.php into the framework program through the following method:

vendor('htmlpurifier.library.HTMLPurifier#auto');

However, I am using ThinkPHP 3.2.1 here and found that this method can only be used in functions. This introduction into the controller class cannot be correctly recognized. In other words, we can only reference it in the common/function.php file.

Create HTMLPurifier object and implement rich text filtering

$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
$clean_html = $purifier->purify($dirty_html);

How to configure the HTMLPurifier filter

To use HTMLPurifier, the focus is still on how to configure it. For the above program, we created a default configuration object through the createDefault() method. If we want to modify the configuration, we can use the set method to configure the settings. The method is as follows:

$config->set('config_object', value, a=null);

The first parameter is the attribute that needs to be configured, the second parameter is the value of the attribute, and the third parameter is specific I haven't figured out what it is used for, but I generally haven't used it. I will study it slowly when I have time.

HTMLPurifier's configuration attributes can be queried through its website

Configuration attribute selection

HTMLPurifier's configuration documents are mainly two-level classifications, and the major categories are Attr (attribute), HTML (html tag), AutoFormat (automatic format), CSS (css configuration), Output (output configuration)... Subcategory selection can be completed by adding the name of the major category. Adding the name of the subcategory.

For example, if I want to configure allowed html tags, such as p tag and a tag, I can configure it as follows

$config->set('HTML.Allowed', 'p,a');

Selection of attribute values

In the official document, click one After the attribute, you can see the explanation of this attribute. It will tell you that the value type (Type) of this attribute is String, Int, Array, Boolen...

Then it will also tell you the default value of this attribute. , such as NULL, true, false, etc. The format of this value is the same as PHP's format.

Whitelist filtering mechanism

HTMLPurifier uses a whitelist filtering mechanism, and only those that are allowed will pass the test.

Basic filtering example

a. Filter out all html tags in the text

/**
 * 过滤掉所有html标签很简单,原因则在白名单机制完成
 */
$config->set('HTML.Allowed', '');

b. Keep the hyperlink tag a and its href link address attribute, and automatically add target The attribute value is '_blank'

$config->set('HTML.Allowed', 'a[href]');
$config->set('HTML.TargetBlank', true);

c, automatically complete the paragraph code and clear out useless empty tags

// 让文本自动添加段落标签,前提是必须允许P标签的使用
$config->set('HTML.Allowed', 'p');
$config->set('AutoFormat.AutoParagraph', true);
// 清除空标签
$config->set('AutoFormat.RemoveEmpty', true);
……


Disclaimer

All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn

Related Article

How Do I Link Static Libraries That Depend on Other Static Libraries? How Do I Link Static Libraries That Depend on Other Static Libraries?

13 Dec 2024

Linking Static Libraries to Other Static Libraries: A Comprehensive ApproachStatic libraries provide a convenient mechanism to package reusable...

How to import third-party libraries in ThinkPHP How to import third-party libraries in ThinkPHP

03 Jun 2023

Third-party class libraries Third-party class libraries refer to other class libraries besides the ThinkPHP framework and application project class libraries. They are generally provided by third-party systems or products, such as class libraries of Smarty, Zend and other systems. For the class libraries imported earlier using automatic loading or the import method, the ThinkPHP convention is to use .class.php as the suffix. Non-such suffixes need to be controlled through the import parameters. But for the third type of library, since there is no such agreement, its suffix can only be considered to be php. In order to easily introduce class libraries from other frameworks and systems, ThinkPHP specifically provides the function of importing third-party class libraries. Third-party class libraries are uniformly placed in the ThinkPHP system directory/

Use jquery.noConflict() to solve the problem of conflicts between jquery library and other libraries Use jquery.noConflict() to solve the problem of conflicts between jquery library and other libraries

20 Jun 2017

When developing with jQuery, you may also use other JS libraries, such as Prototype, but conflicts may occur when multiple libraries coexist; if conflicts occur, you can solve them through the following solutions: 1. jQuery libraries in other Import the library before and use the jQuery (callback) method directly such as:

What are linux dependency packages What are linux dependency packages

24 Mar 2023

Linux dependency packages refer to "library files". Most dependency packages are library files, including dynamic libraries and static libraries. Linux systems, like other operating systems, are modular in design, which means that functions depend on each other, and some Functions require some other functions to support them, which can improve code reusability.

How to use pip tool in python How to use pip tool in python

02 Jul 2019

After installing python, if you need to install some other libraries, there are generally two methods. One is to manually download and install them from the official website of each library; the other method is to install pip. Using pip can easily install various python libraries. library. After installing pip, you can directly install and delete third-party libraries through commands.

How popular libraries and frameworks in the C++ ecosystem compare to other programming language ecosystems How popular libraries and frameworks in the C++ ecosystem compare to other programming language ecosystems

02 Jun 2024

Popular libraries and frameworks in the C++ ecosystem compared to other programming language ecosystems Introduction C++ is a language critical to systems programming, with an extensive and robust ecosystem of libraries and frameworks. This article will compare libraries and frameworks in the C++ ecosystem with those in the ecosystem of other popular programming languages, focusing on features, performance, and ease of use. Library Standard Library: The C++ standard library provides a wide range of data structures, algorithms, and input/output functions. Although other languages ​​have similar standard libraries, C++'s libraries are known for their efficiency and flexibility. Boost Library: Boost is a widely used collection of third-party libraries that provide many additional features not found in the standard library. It is known for providing various cross-platform features such as threading,

See all articles