Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the php plug-in HTMLPurifier

Shulou Source: shulou.com Published: 2022-06-03 06:42:44 10月02日 Update

This article mainly introduces how to use php plug-in HTMLPurifier, has a certain reference value, interested friends can refer to it, I hope you have a lot to gain after reading this article, let Xiaobian take you to understand it together.

Use of HTMLPurifier Plugin

Download HTMLPurifier Plugin

The useful part of the HTMLPurifier plugin is the library

Using HTMLPurifier library

the first way

The copy code is as follows:

or

The copy code is as follows:

An example given on the website is

The copy code is as follows:

require_once 'HTMLPurifier.auto.php';

My colleagues often use

The copy 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 copy 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 (standing)

$config->set ('Core.Encoding', ' UTF-8')//character encoding (permanent)

HTML allowed elements

div element, table element, tr element, td element, br element

new HTMLPurifier object

The copy code is as follows:

$purifier = new HTMLPurifier($config);

Call the purify method of the HTML Purifier object

The copy code is as follows:

$puri_html = $purifier->purify($html);

the second way

Custom a class HtmlPurifier.php

The copy code is as follows:

Set config information

For example:

The code is as follows:

$conf = array(

array('HTML.AllowedElements',

array(

'div' => true,

'table' => true,

'tr' => true,

'td' => true,

'br' => true,

),

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

array ('HTML.AllowedAttributes', array ('class' => TRUE), false), //AllowedAttributes class

array ('Attr. ForbiddenClasses', array ('resume_p' => TRUE), false), //forbid classes such as

array ('AutoFormat. RemoveEmpty', true, false), //remove spaces

array('AutoFormat.RemoveEmpty.RemoveNbsp', true, false), //Go nbsp

array('URI.Disable', true, false),

);

call

The code is as follows:

$p = new Resume_HtmlPurifier($conf);

$puri_html = $p->filter($html);

Thank you for reading this article carefully. I hope that Xiaobian will share the article "How to use php plugin HTMLPurifier". This article is helpful to everyone. At the same time, I hope that everyone will support you a lot. Pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

Tags: Code elements plug-ins articles examples objects attributes methods permanent value information interests colleagues at the same time characters commonly used documents methods more useful Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Docker Huawei macOS Shulou Information