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
232
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
1
Views
69
Replies
0
Views
87
Replies
0
Views
151
Replies
3
Views
374
Recommended threads
Replies
2
Views
4,744
Replies
1
Views
466
Replies
6
Views
4,761
Replies
3
Views
3,284

Latest postsNew 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