Just wanted to give the DOMDocument (docs) version, since the conventional wisdom says "Don't use RegEx on HTML!!". Well, that's a fine thing to say, but then what!? Well, here you go:
// create a new DOMDocument
$doc = new DOMDocument();
// load the string into the DOM
$doc->loadHTML('Some test <a href="site">here</a>. Yet <a href="site2">another</a> test.');
// since we are working with HTML fragments here, remove <!DOCTYPE
$doc->removeChild($doc->firstChild);
// likewise remove <html><body></body></html>
$doc->replaceChild($doc->firstChild->firstChild->firstChild, $doc->firstChild);
//Loop through each <a> tag in the dom and wrap it with <noindex>
foreach($doc->getElementsByTagName('a') as $link) {
$parent = $link->parentNode;
$ni = $doc->createElement('noindex');
$ni->appendChild($link->cloneNode(true));
$parent->replaceChild($ni, $link);
}
echo $doc->saveHTML();
Check it out here: http://codepad.org/ANi93sBj