Add rel="nofollow" for all links with PHP?

bob

Well-known member
Registered
Joined
Apr 22, 2014
Messages
92
Points
0
I'm looking for a PHP function to add rel="nofollow" for all links for a specific part on my site?

If possible, give me any suggestions. thanks in advanced.
 

webdesign

Well-known member
Joined
Jul 5, 2012
Messages
120
Points
0
Hi Bob

This function allow you add rel="nofollow" automatically to your content.
If existed rel, skip
Try this code with your PHP 5.3+ version
Code:
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
    );
}
How to use:
Code:
echo nofollow('<a href="http://www.yourdomain.com" rel="nofollow">your text</a>');
// will be same because it's already contains rel="nofollow" parameter

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

echo nofollow('<a href="http://www.yourdomain.com">your text</a>', 'yourdomain');
// skip this link as internall link
Hope it helps!
 
Older Threads
Latest Threads

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top