溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php插件HTMLPurifier怎么用

發(fā)布時(shí)間:2021-10-12 11:31:49 來(lái)源:億速云 閱讀:128 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了php插件HTMLPurifier怎么用,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

HTMLPurifier插件的使用
下載HTMLPurifier插件
HTMLPurifier插件有用的部分是 library

php插件HTMLPurifier怎么用
使用HTMLPurifier library類庫(kù)
第一種方式

復(fù)制代碼 代碼如下:


<?php
require_once 'HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
?>


或者

復(fù)制代碼 代碼如下:


<?php
require_once 'HTMLPurifier.includes.php';
require_once 'HTMLPurifier.autoload.php';
$config = HTMLPurifier_Config::createDefault();
?>


官網(wǎng)給出的例子是

復(fù)制代碼 代碼如下:


require_once 'HTMLPurifier.auto.php';


我同事常用的是

復(fù)制代碼 代碼如下:


require_once 'HTMLPurifier.includes.php';
require_once 'HTMLPurifier.autoload.php';


設(shè)置$config
configdoc
http://htmlpurifier.org/live/configdoc/plain.html
例子

復(fù)制代碼 代碼如下:


$config->set('HTML.AllowedElements', array('div'=>true, 'table'=>true, 'tr'=>true, 'td'=>true, 'br'=>true));
$config->set('HTML.Doctype', 'XHTML 1.0 Transitional')  //html文檔類型(常設(shè))
$config->set('Core.Encoding', 'UTF-8')   //字符編碼(常設(shè))


HTML允許的元素
div元素,table元素,tr元素,td元素,br元素
new HTMLPurifier對(duì)象

復(fù)制代碼 代碼如下:


$purifier = new HTMLPurifier($config);


調(diào)用HTMLPurifier對(duì)象的purify方法

復(fù)制代碼 代碼如下:


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


第二種方式
自定義一個(gè)類 HtmlPurifier.php

復(fù)制代碼 代碼如下:


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

 }
}
?>


設(shè)置config信息
例如:

代碼如下:


$conf = array(
 array('HTML.AllowedElements',
           array(
                     'div' => true,
                     'table' => true,
                     'tr' => true,
                     'td' => true,
                     'br' => true,
                 ),
                 false), //允許屬性 div table tr td br元素
         array('HTML.AllowedAttributes', array('class' => TRUE), false),  //允許屬性 class
         array('Attr.ForbiddenClasses', array('resume_p' => TRUE), false), //禁止classes如
         array('AutoFormat.RemoveEmpty', true, false),    //去空格
         array('AutoFormat.RemoveEmpty.RemoveNbsp', true, false),  //去nbsp
         array('URI.Disable', true, false),
);


調(diào)用

代碼如下:

$p = new Resume_HtmlPurifier($conf);
$puri_html = $p->filter($html);

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“php插件HTMLPurifier怎么用”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI