PHP正则表达式将rel =“nofollow”添加到外部链接[重复]

This question already has an answer here:

I need to add rel="nofollow" to all external links (not leading to my site or its subdomains).

I have done this in two steps, at first I add rel="nofollow" to all links (even internal links) using the following regular expression:

<a href="http([s]?)://(.*?)"

Then in the second step I eliminate rel="nofollow" for internal links (my site and its subdomains) using the following regular expression:

<a href="http([s]?)://(www\.|forum\.|blog\.)mysite.com(.*?)" rel="nofollow"

How can I do this only in one step? Is it possible?

</div>

The DOM way:

$doc = new DOMDocument();
@$doc -> loadHTMLFile($url); // url of the html file
$links = $doc->getElementsByTagName('a');

foreach($links as $link) {
    $href = $link->getAttribute('href');
    if (preg_match('~^https?://(?>[^/m]++|m++(?!ysite.com\b))*~', $href))
        $link->setAttribute('rel', 'nofollow');
}

$doc->saveHTMLFile($url);