user503853
user503853

Reputation:

How to get links not from a website using php and regular expression

I want to add rel="nofollow" in all links in my website if the links link to other website.

For example,

$str = "<a href='www.linktoothersite.com'>I swear this isn't spam!</a><br><a href='www.mywebsite.com'>Hello World</a>";

The output should be

$str = "<a href='www.linktoothersite.com' rel="nofollow">I swear this isn't spam!</a><br><a href='www.mywebsite.com'>Hello World</a>";

I really want to regular expression but not DDOMDocument. Because when I using DOMDocument I always got error " Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: htmlParseEntityRef: expecting ';' in Entity"

Upvotes: 1

Views: 456

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270617

Use a DOM parser and loop over all the links, checking their href attribute for other sites. This is untested and might require some tweaking.

// assuming your html is in $HTMLstring
$dom = new DOMDocument();
$dom->loadHTML($HTMLstring);

// May need to disable error checking if the HTML isn't fully valid
$dom->strictErrorChecking = FALSE;

// Get all the links
$links = $dom->getElementsByTagName("a");
foreach($links as $link) {
  $href = $link->getAttribute("href");

  // Find out if the link points to a domain other than yours
  // If your internal links are relative, you'll have to do something fancier to check
  // their destinations than this simple strpos()
  if (strpos("yourdomain.example.com", $href) == -1) {
     // Add the attribute
     $link->setAttribute("rel", "nofollow");
  }

// Save the html
$output = $dom->saveHTML;

Upvotes: 4

Related Questions