Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7664481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:19:01+00:00 2026-05-31T14:19:01+00:00

I am trying to configure HTMLPurifier to only display external links as plain text.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:19:02+00:00Added an answer on May 31, 2026 at 2:19 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to configure the Quick Launch menu to only display the ancestors and
I am trying to configure MySQL to be used with PHP and Apache. But
I'm trying to configure Apache to allow read only access and ask for user
I am trying to configure Cruise Control with Nunit for reporting purpose. But I
I am trying to configure the NumberPicker widget using API 14 . There are
I'm trying to configure EhCache with JGroups-based replication, but I get log flooded with
I've been trying to configure the new Facebook Javascript API with my website, but
I am trying to configure JSF+Spring+hibernate and I'm tying to run a test but
i am trying to configure my laptop with my old keys. However there is
I'm trying to configure Team Foundation Reporting but without any success. The App Tier

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.