jb7AN
jb7AN

Reputation: 87

PHP: Detect and correct outgoing links in HTML

i need a function which corrects ALL outgoing links within a given HTML-Text and adds the attribute "rel=nofollow" to the link. Only outgoing links should be corrected.

Example: My domain is www.laptops.com

$myDomain = "www.laptops.com";

$html = 
 "Hello World have a look at <a href="www.laptops.com/apple">Apple Laptops</a>. 
  For more ino go to <a href="www.apple.com">Apple.com</a> 
  or to <a href="www.appleblog.com">Appleblog.com</a>";

function correct($html,$myDomain){ 
    //get all links by filtering '<a ... href="{$link}" .....>' and 
    //check with isOutgoing($href,$myDomain )
}

$newHTML = correct($html,$myDomain);

echo $newHTML;

//Hello World have a look at <a href="www.laptops.com/apple">Apple Laptops</a>. 
//For more ino go to <a rel="nofollow" href="www.apple.com">Apple.com</a> 
//or to <a rel="nofollow" href="www.appleblog.com">Appleblog.com</a> 

So far i have a function "isOutgoing($link)", which can detect, if a link is outgoing or not, but the detection of ALL "< a ... href="{$link}" ..... > " parts of the HTML-Text and filtering the {$link} makes problems. I know that it should be possible with preg_match(), but i have no idea how to solve it.

Upvotes: 0

Views: 267

Answers (2)

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

You should avoid using regex, instead, you should use DOMDocument and DOMXPath.

<?php
$dom = new DOMDocument();

$dom->loadHtml('
Hello World have a look at <a href="www.laptops.com/apple">Apple Laptops</a>. 
  For more ino go to <a href="www.apple.com">Apple.com</a> 
  or to <a href="www.appleblog.com">Appleblog.com</a>
', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$xpath = new DOMXPath($dom);

foreach ($xpath->query("//a") as $link) {
    $href = $link->getAttribute('href');

    // link does not have a www.laptops.com in it, add rel attribute
    if (strpos($href, 'www.laptops.com') === false) {
        $link->setAttribute("rel", "nofollow noopener");
    }
}

echo $dom->saveHTML();

Result:

<p>Hello World have a look at <a href="www.laptops.com/apple">Apple Laptops</a>. 
  For more ino go to <a href="www.apple.com" rel="nofollow noopener">Apple.com</a> 
  or to <a href="www.appleblog.com" rel="nofollow noopener">Appleblog.com</a>
</p>

https://3v4l.org/DseDi

Upvotes: 2

delboy1978uk
delboy1978uk

Reputation: 12365

This would would be so much easier with a bit of jQuery.

<script type="text/javascript">
$(document).ready(function(){
    $('a').each(function(){
        let href = $(this).prop('href');
        if (!href.search('laptops.com')) {
            $(this).prop('rel', 'nofollow');
        }
    });
});
</script>

Upvotes: 0

Related Questions