如果没有使用PHP存在标题标记,则添加nofollow属性以进行链接

I have a bunch of text with html in it. Basically what I want to do is for all links found in this text I want to add a rel="noindex" to every link found only if the title attribute is no present.

For example if a link looks like this:

<a href="test.html">test</a>

I want it to look like:

<a rel="nofollow" href="test.html">test</a>

But if the link looks like this:

<a title="test title" href="test.html">test</a>

I dont want to add the rel="nofollow" attribute to that. How can I do that in php?

EDIT:

Im sorry I didnt mention this but I am using PHP4. Yes I know but Im stuck with PHP4.

First use preg match to get if title is added.

$str = '<a href="test.html">test</a>';
if(!preg_match('/title=/', $str))
{
    $str = str_replace('href=', 'rel="nofollow" href=', $str);
}

Quite simply with DOMDocument:

$dom = new DOMDocument;
$dom->loadHTML($yourHTML);

$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
    if (!$link->hasAttribute('title')) {
        $link->setAttribute('rel', 'nofollow');
    }
}

$yourHTML = $dom->saveHTML();

This is far more stable and reliable than mucking about with regex.