Adding rel = "nofollow" when saving data

I have my application that allows users to write comments on my website. His work is wonderful. I also have a tool to insert my web links into it. I feel good with content with my web links.

Now I want to add rel = "nofollow" for all links to the content that they were written.

I would like to add rel = "nofollow" using php ie when saving data.

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

a good example will be very effective

+5
source share
3

Regexs HTML, PHP HTML-, .

nofollow, rel .

$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.

HTML $dom->saveHTML(). , html, body .., , HTML...

$html = '';

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

echo $html;

>= PHP 5.3, saveXML() saveHTML() .

HTML...

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

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

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

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

... ...

<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>
+18
. , . :
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;      
}

:

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

 $str = add_no_follow($str);
+2

I copied Alex's answer and turned it into a function that makes nofollow links and is open in a new tab / window (and added UTF-8 support). I'm not sure if this is the best way to do this, but it works (constructive input is welcome):

function nofollow_new_window($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));

    $target = array(); 

    if ($anchor->hasAttribute('target') AND ($relAtt = $anchor->getAttribute('target')) !== '') {
       $target = preg_split('/\s+/', trim($relAtt));
        }

    if (in_array('_blank', $target)) {
      continue;
        }

    $target[] = '_blank';
    $anchor->setAttribute('target', implode(' ', $target));
    }

$str = utf8_decode($dom->saveHTML($dom->documentElement));
return $str;
}

Just use the function as follows:

$str = '<html><head></head><body>fdsafffffdfsfdffff dfsdaff flkklfd aldsfklffdssfdfds <a href="http://www.google.com">Google</a></body></html>';

$str = nofollow_new_window($str);

echo $str;
0
source

All Articles