HTML外部为文本,将HTMLPurifier配置为以纯文本的形式显示外部链接

好吧,我成功地定制注射器添加到HTMLPurifier,那就是:

首先,在创建 “DisplayRemoteLinkURI.php”“包括\ htmlpurifier \库\ HTMLPurifier \注射器“写这在它

class HTMLPurifier_Injector_DisplayRemoteLinkURI extends HTMLPurifier_Injector

{

public $name = 'DisplayRemoteLinkURI';

public $needed = array('a');

public function handleElement(&$token) {

}

public function handleEnd(&$token) {

if (isset($token->start->attr['href'])){

$url = $token->start->attr['href'];

if($this->is_remote($url)){

unset($token->start->attr['href']);

$token = array($token, new HTMLPurifier_Token_Text(" ($url)"));

}

} else {

// nothing to display

}

}

public function is_remote($path){

$urlvar = parse_url($path);

$remote_schemes = array("mailto");

$local_schemes = array("javascript");

if(in_array($urlvar["scheme"],$remote_schemes)){

return true;

}else if(in_array($urlvar["scheme"],$local_schemes)){

return false;

}else{

if(empty($urlvar["host"]) || $urlvar["host"]==$_SERVER["HTTP_HOST"]){

return false;

}else{

return true;

}

}

}

}

?>

然后创建一个名为另一个文件” AUT oFormat.DisplayRemoteLinkURI.txt “中的”包括\ htmlpurifier \库\ HTMLPurifier \ ConfigSchema \模式“,并补充一点:

AutoFormat.DisplayRemoteLinkURI

TYPE: bool

VERSION: 3.2.0

DEFAULT: false

--DESCRIPTION--

This directive turns on the in-text display of Remote URIs in <a> tags, and disables

those links. For example, example becomes

example (http://example.com).

--# vim: et sw=4 sts=4

之后,添加此行

require 'HTMLPurifier/Injector/DisplayRemoteLinkURI.php';

require 'HTMLPurifier/Injector/DisplayLinkURI.php';

in include \ htmlpurifier \ library \ HTMLPurifier.includes.php

然后,添加这一行

require_once $__dir . '/HTMLPurifier/Injector/DisplayRemoteLinkURI.php';

require_once $__dir . '/HTMLPurifier/Injector/DisplayLinkURI.php';

在包括\ htmlpurifier \库\ HTMLPurifier.safe-包括。php

经过这些编辑,如果你的文件在本地,运行cmd.exe并进入你的php目录。然后从php.exe运行“include/HTMLPurifier/maintenance/generate-schema-cache.php”。

或者如果你想通过浏览器做到这一点,重命名你的.htaccess文件内的“include/HTMLPurifier/maintenance /”一段时间,然后将此行添加到“generate-schema-cache.php” <?php标签后的第一行 ;

php_set_env("PHP_IS_CLI",true);

然后从浏览器运行此文件。在看到“保存模式完成!”后,重命名您的.htaccess文件。

然后在您的脚本中,使用“AutoFormat.DisplayRemoteLinkURI”作为配置,瞧!

请注意,我在这里给出的第一个文件中的is_remote()函数可能不太好,而且我找不到检查链接是远程还是本地链接的脚本,因此如果您以后可能会更改该链接需要。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值