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 ie 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
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.
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>
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);
I've copied Alex's answer and made it into a function that makes links nofollow and 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;
}
Simply use the function like this:
$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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.