apply rel="nofollow" attributes to all external links

function nofollow($html, $skip = null) { return preg_replace_callback( "#(<a[^>]+?)>#is", function ($mach) use ($skip) { return ( !($skip && strpos($mach[1], $skip) !== false) && strpos($mach[1], 'rel=') === false ) ? $mach[1] . ' rel="nofollow">' : $mach[0]; }, $html ); }
Examples:
echo nofollow('<a href="link somewhere" rel="something">something</a>');
// will be same because it's already contains rel parameter

echo nofollow('<a href=" http://www.cnn.com">something</a>'); // ad
// add rel="nofollow" parameter to anchor

echo nofollow('<a href="http://localhost">something</a>', 'localhost');
// skip this link as internall link

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.