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
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.
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 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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With