Configuring HTMLPurifier to Display External Links as Plain Text

I am trying to set up an HTMLPurifier to only display external links as plain text. I used the DisplayLinkURI parameter, but it displays all links as plain text. is there any configuration for this? here is my code:

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


Output signal

<a>mysite</a> (
<a>external</a> (


I want the result to be like this:

<a href="">mysite</a>
<a>external</a> (


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


source to share

3 answers

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

First create " DisplayRemoteLinkURI.php" in " include \ htmlpurifier \ library \ HTMLPurifier \ Injector " and write in it


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'];
                $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");

             return true;
        }else if(in_array($urlvar["scheme"],$local_schemes)){
             return false;
             if(empty($urlvar["host"]) || $urlvar["host"]==$_SERVER["HTTP_HOST"]){
                  return false;
                  return true;



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

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


After that add this line

require 'HTMLPurifier/Injector/DisplayRemoteLinkURI.php';



require 'HTMLPurifier/Injector/DisplayLinkURI.php';


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

Then add this line

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



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


in include \ htmlpurifier \ library \

After these changes, if your files are on your local machine, run cmd.exe and change to the php directory. Then run "include / HTMLPurifier / maintenance / generate-schema-cache.php" from php.exe.

Or if you want to do it through the browser, rename the .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 tag <?php




and then run this file from browser. After you see "Saving the schema .. done!", Rename the .htaccess file .

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

Note that the is_remote () function inside the first file I brought here may not be very good and I could not find a script that checks if a link is remote or local, so you can change it later if you need ...



I believe this is the one you are looking for



There is an option named URI.DisableExternal and AutoFormat.Linkify. Set them to TRUE and see what happens.

And AutoFormat.DisplayLinkURI disables all links. I suggest you use both of the above and not AutoFormat.DisplayLinkURI.



All Articles