loadHTML($str); $anchors = $dom->getElementsByTagName('a'); foreach($anchors as $anchor) { $rel = array(); if ($anchor->hasAttribute('rel') AND ($relAtt = $anchor->getAttribute('rel')) !== '') { $rel = preg_split('/\s+/', trim($relAtt)); } if (in_array('nofollow', $rel)) { continue; } $rel[] = 'nofollow'; $anchor->setAttribute('rel', implode(' ', $rel)); } $dom->saveHTML(); $html = ''; foreach($dom->getElementsByTagName('body')->item(0)->childNodes as $element) { $html .= $dom->saveXML($element, LIBXML_NOEMPTYTAG); } return $html; } ?>