rewrite/mask external link inside article

2019-08-15 09:27发布

问题:

I want to rewrite/mask all external url in my article and also add nofollow and target="_blank". So that original link to the external site is get encrypted/ masked/ rewritten.

For example:

original link: www.google.com
rewrite it to: www.mydomain.com?goto=google.com

There is a plugin for joomla which rewrite external link: rewrite plugin.

But I am not using joomla. Please have a look at above plugin, It does exactly what I am looking for.

What I want?

$article = "hello this is example article I want to replace all external link http://google.com";
$host = substr($_SERVER['HTTP_HOST'], 0, 4) == 'www.' ? substr($_SERVER['HTTP_HOST'], 0) : $_SERVER['HTTP_HOST'];

if (thisIsNotMyWebsite){
    replace external url

}

回答1:

You can use DOMDocument to parse and traverse the document.

function rewriteExternal($html) {

        // The url for your redirection script
        $prefix = 'http://www.example.com?goto=';

        // a regular expression to determine if
        // this link is within your site, edit for your
        // domain or other needs
        $is_internal = '/(?:^\/|^\.\.\/)|example\.com/';

    $dom = new DOMDocument();

    // Parse the HTML into a DOM
    $dom->loadHTML($html);

    $links = $dom->getElementsByTagName('a');

    foreach ($links as $link) {
            $href = $link->getAttribute('href');
            if (!preg_match($is_internal, $href)) {
                $link->getAttributeNode('href')->value = $prefix . $href;
                $link->setAttributeNode(new DOMAttr('rel', 'nofollow'));
                $link->setAttributeNode(new DOMAttr('target', '_blank'));
            }
    }

    // returns the updated HTML or false if there was an error
    return $dom->saveHTML();
}

This approach will be much more reliable than using a regular expression based solution since it actually parses the DOM for you instead of relying on a often-fragile regex.



回答2:

something like:

<?php
 $html ='1224 <a href="http://www.google.com">google</a> 567';
$tracking_string = 'http://example.com/track.php?url=';
$html = preg_replace('#(<a[^>]+href=")(http|https)([^>" ]+)("?[^>]*>)#is','\\1'.$tracking_string.'\\2\\3\\4',$html);
echo $html; 

in action here: http://codepad.viper-7.com/7BYkoc

--my last update

<?php
$html =' 1224 <a href="http://www.google.com">google</a> 567';

$tracking_string = 'http://example.com/track.php?url=';
$html = preg_replace('#(<a[^>]+)(href=")(http|https)([^>" ]+)("?[^>]*>)#is','\\1 nofollow  target="_blank" \\2'.$tracking_string.'\\3\\4\\5',$html);

echo $html;

http://codepad.viper-7.com/JP8sUk