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.