1

如果链接链接到其他网站,我想在我网站的所有链接中添加 rel="nofollow"。

例如,

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

输出应该是

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

我真的想要正则表达式而不是 DDOMDocument。因为当我使用 DOMDocument 时,我总是收到错误“警告:DOMDocument::loadHTML() [domdocument.loadhtml]: htmlParseEntityRef: expecting ';' 在实体"

4

1 回答 1

4

使用 DOM 解析器并遍历所有链接,检查href其他站点的属性。这是未经测试的,可能需要一些调整。

// 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;
于 2011-06-24T20:07:38.880 回答