Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding rel="nofollow" while saving data

I have my application to allow users to write comments on my website. Its working fine. I also have tool to insert their weblinks in it. I feel good with contents with their own weblinks.

Now i want to add rel="nofollow" to every links on content that they have been written.

I would like to add rel="nofollow" using php i.e while saving data.

So what's a simple method to add rel="nofollow" or updated rel="someother" with rel="someother nofollow" using php

a nice example will be much efficient

like image 630
KoolKabin Avatar asked May 01 '11 12:05

KoolKabin


People also ask

Where would you place the REL nofollow?

When a link exists on your site primarily for profit, it should be assigned a rel="nofollow" attribute. Simply put: If you publish paid advertisements on your site — banner ads, text links, sponsored content, affiliate links, etc. — the links going to your advertisers' sites should be rel=”nofollow” links.

What does add nofollow mean?

Nofollow tags explained The inclusion of a nofollow tag instructs the search engines NOT to visit the site or rather not to ascribe credit to boost the ranking of the destination site based on the link.

What is rel nofollow in HTML?

What Are Nofollow Links? Nofollow links are links with a rel=”nofollow” HTML tag applied to them. The nofollow tag tells search engines to ignore that link. Because nofollow links do not pass PageRank they likely don't impact search engine rankings.


2 Answers

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.

$dom = new DOMDocument;

$dom->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));
}

var_dump($dom->saveHTML());

CodePad.

The resulting HTML is in $dom->saveHTML(). Except it will wrap it with html, body elements, etc, so use this to extract just the HTML you entered...

$html = '';

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

echo $html;

If you have >= PHP 5.3, replace saveXML() with saveHTML() and drop the second argument.

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>

...is converted into...

<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>
like image 180
alex Avatar answered Sep 25 '22 01:09

alex


Good Alex. If it is in the form of a function it is more useful. So I made it below:


function add_no_follow($str){ 
  $dom = new DOMDocument;

  $dom->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;      
}

Use as follows :

  $str = "Some content with link Some content  ... ";

 $str = add_no_follow($str);
like image 40
Withfriendship Hiox Avatar answered Sep 27 '22 01:09

Withfriendship Hiox