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

bob

New member
Joined
Apr 22, 2014
Messages
203
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

New member
Joined
Jul 5, 2012
Messages
234
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!
 
Newer threads
Latest threads
Replies
0
Views
18
Replies
1
Views
42
Recommended threads

Referral contests

Referral link for :

Sponsors

Popular tags

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