doufan1899 2012-03-19 06:37 采纳率: 100%
浏览 100
已采纳

配置HTMLPurifier以将外部链接显示为纯文本

I am trying to configure HTMLPurifier to only display external links as plain text. I used DisplayLinkURI option but it display all links as a plain text. is there any configuration for that? here is my code:

$mySite='<a href="http://www.mysite.com/">mysite</a>';
$externalSite='<a href="http://www.external.com/">external</a>';
 require_once 'include/htmlpurifier/library/HTMLPurifier.auto.php';
                        $Config = HTMLPurifier_Config::createDefault();
                        $Config->set('AutoFormat.DisplayLinkURI', true);
                        $purifier = new HTMLPurifier($Config);
                        $mySite= $purifier->purify($mySite);
                        $externalSite=$purifier->purify($externalSite);                   
                        echo $mySite;
                        echo $externalSite;

The output is

<a>mysite</a> (http://www.mysite.com/)
<a>external</a> (http://www.external.com/)

I want the output to be like this:

<a href="http://www.mysite.com/">mysite</a>
<a>external</a> (http://www.external.com/)

Update: I want to keep external links for images without change. I only need to convert hyperlinks to plain text.

  • 写回答

3条回答 默认 最新

  • dreamlife2014 2012-03-21 12:08
    关注

    Ok, I succeeded to add a custom injector to HTMLPurifier, here it is:

    First, Create a "DisplayRemoteLinkURI.php" in "include\htmlpurifier\library\HTMLPurifier\Injector" and write this in it

    <?php 
    
    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;
                 }
            }
        }
    }
    
    ?>
    

    And then create another file named "AutoFormat.DisplayRemoteLinkURI.txt" in "include\htmlpurifier\library\HTMLPurifier\ConfigSchema\schema" and add this :

    AutoFormat.DisplayRemoteLinkURI
    TYPE: bool
    VERSION: 3.2.0
    DEFAULT: false
    --DESCRIPTION--
    <p>
      This directive turns on the in-text display of Remote URIs in &lt;a&gt; tags, and disables
      those links. For example, <a href="http://example.com">example</a> becomes
    example (<a>http://example.com</a>).
    </p>
    --# vim: et sw=4 sts=4
    

    After that, Add this line

    require 'HTMLPurifier/Injector/DisplayRemoteLinkURI.php';
    

    under

    require 'HTMLPurifier/Injector/DisplayLinkURI.php';
    

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

    Then, Add this line

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

    under

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

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

    After these edits, if your files are at local, run cmd.exe and go to your php directory. Then run "include/HTMLPurifier/maintenance/generate-schema-cache.php" from php.exe.

    Or if you want to do this via browser, rename your .htaccess file inside "include/HTMLPurifier/maintenance/" to something else for a while, then add this line inside "generate-schema-cache.php" on the first line after the <?php tag;

    php_set_env("PHP_IS_CLI",true);
    

    and then run this file from browser. After you see "Saving schema.. done!", rename your .htaccess file back.

    Then in your script, use "AutoFormat.DisplayRemoteLinkURI" as config, and voila!

    Note that the is_remote() function inside the first file I gave here might be not so good, and I couldn't find a script that checks if a link is remote or local, so you might alter it later if you need.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line