Have an account? Sign in
Login  Register  Facebook
This Page is Under Construction! - If You Want To Help Please Send your CV - Advanced Web Core (BETA)
[Edit] Adding rel="nofollow" by DOMDocument
Regexs really aren't the best tool for dealing with HTML, especially when PHP has a pretty good HTML parser built in.
This code will handle adding nofollow if the rel attribute is already populated.
Example:This HTML
<a href="#">hello</a>

<a href="#" rel="">hello</a>

<a href="#" rel="hello there">hello</a>

<a href="#" rel="nofollow">hello</a>
will be
<a href="#" rel="nofollow">hello</a>

<a href="#" rel="nofollow">hello</a>

<a href="#" rel="hello there nofollow">hello</a>

<a href="#" rel="nofollow">hello</a>
function add_no_follow($str){ 
  $dom = new DOMDocument;


  $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)) {

      $rel[] = 'nofollow';
      $anchor->setAttribute('rel', implode(' ', $rel));


  $html = '';

  foreach($dom->getElementsByTagName('body')->item(0)->childNodes as $element) {
      $html .= $dom->saveXML($element, LIBXML_NOEMPTYTAG);

  return $html;