current location: Home > download site > Library download > Other libraries > htmlpurifierPHP filtering library
htmlpurifierPHP filtering library
Classify: Library download / Other libraries | Release time: 2017-12-19 | visits: 4369 |
Download: 80 |
Latest Downloads
Fantasy Aquarium
Girls Frontline
Wings of Stars
Little Flower Fairy Fairy Paradise
Restaurant Cute Story
Shanhe Travel Exploration
Love and Producer
The most powerful brain 3
Odd Dust: Damila
Young Journey to the West 2
24 HoursReading Leaderboard
- 1 How to Prevent Labels from Being Cut Off in Matplotlib Plots?
- 2 How to Sort an Embedded Array Field in MongoDB?
- 3 Movavi Video Editor Cracked
- 4 dynamics.exe - What is dynamics.exe?
- 5 How to Wait for Asynchronous Callback Functions Simultaneously?
- 6 What is the Purpose of `` in Backbone.js?
- 7 How to Achieve the Functionality of Postgresql's DISTINCT ON in MySQL?
- 8 How to Escape the Colon Character in JPA Queries for MySQL User Variables?
- 9 How to Retrieve the Initial Row of Each Group in a Pandas DataFrame?
- 10 How to Fix the "EPERM: operation not permitted" Error When Setting npm Prefix on Windows?
- 11 dwrcc.exe - What is dwrcc.exe?
- 12 How to Accurately Determine Perfect Squares Without Floating-Point Errors?
- 13 Why are `navigator`, `window`, and `document` undefined in my Nuxt application?
- 14 dxinput3.dll - What is dxinput3.dll?
- 15 Why Does Android Studio Throw the "getSlotFromBufferLocked: unknown buffer" Error During Registration on Marshmallow?
Latest Tutorials
-
- Go language practical GraphQL
- 2002 2024-04-19
-
- 550W fan master learns JavaScript from scratch step by step
- 3420 2024-04-18
-
- Getting Started with MySQL (Teacher mosh)
- 1804 2024-04-07
-
- Mock.js | Axios.js | Json | Ajax--Ten days of quality class
- 2617 2024-03-29
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); ……