How to Add Rel="Nofollow" to Links with Preg_Replace()

How to add rel=nofollow to links with preg_replace()

Try to make it more readable first, and only afterwards make your if rules more complex:

function save_rseo_nofollow($content) {
$content["post_content"] =
preg_replace_callback('~<(a\s[^>]+)>~isU', "cb2", $content["post_content"]);
return $content;
}

function cb2($match) {
list($original, $tag) = $match; // regex match groups

$my_folder = "/hostgator"; // re-add quirky config here
$blog_url = "http://localhost/";

if (strpos($tag, "nofollow")) {
return $original;
}
elseif (strpos($tag, $blog_url) && (!$my_folder || !strpos($tag, $my_folder))) {
return $original;
}
else {
return "<$tag rel='nofollow'>";
}
}

Gives following output:

[post_content] =>
<a href="http://localhost/mytest/">internal</a>
<a href="http://localhost/mytest/go/hostgator" rel=nofollow>internal cloaked link</a>
<a href="http://cnn.com" rel=nofollow>external</a>

The problem in your original code might have been $rseo which wasn't declared anywhere.

Modify all links in HTML Purifier

Actually I found partial solution on one of the links on the forum.

This is what I need to do:

$config->set('HTML.Nofollow', true);
$config->set('HTML.TargetBlank', true);

So the full thing looks like this:

$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Nofollow', true);
$config->set('HTML.TargetBlank', true);
$config->set('HTML.Allowed', 'a,b,strong,i,em,u');
$purifier = new HTMLPurifier($config);

Convert plain text URLs into HTML hyperlinks in PHP

Here is another solution, This will catch all http/https/www and convert to clickable links.

$url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i'; 
$string = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $string);
echo $string;

Alternatively for just catching http/https then use the code below.

$url = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';   
$string= preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $string);
echo $string;

EDIT:
The script below will catch all URL types and convert them to clickable links.

$url = '@(http)?(s)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@';
$string = preg_replace($url, '<a href="http$2://$4" target="_blank" title="$0">$0</a>', $string);
echo $string;

The new update, If you're having the string strip the (s) then use the below code block, Thanks to @AndrewEllis for pointing this out.

$url = '@(http(s)?)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@';
$string = preg_replace($url, '<a href="http$2://$4" target="_blank" title="$0">$0</a>', $string);
echo $string;

Here's a very simple solution for the URL not displaying correctly.

$email = '<a href="mailto:email@email.com">email@email.com</a>';
$string = $email;
echo $string;

It is a very simple fix but you will have to modify it for your own purpose.

I've provided multiple answers as some servers are set up differently, so one answer may work for some but not for others, but I hope the answer(s) work for you and if not then let me know, and hopefully, I can come up with another solution.

There are multiple scripts as some PHP files require different scripts also some servers are set up differently, Plus each has different requirements, Some want just HTTP/S, some want WWW and some want FTP/S, Each one will work depending on how the users own scripts are set up, I provided some text with each one with what they do.

How can I add rel=follow to specific domains manually when setting all external links with rel=nofollow?

Ive set up a client side script using jQuery that I think meets the requirements you requested.

$.setAllowExternalLinksFollowed = function(externalLinkWhitelistArray) {
"use strict";
var $externalDomLinks = $('a[href^="http"]');
$externalDomLinks.each(function() {
var $linkInstance = $(this);
// Nofollow all ext links
$linkInstance.attr('rel', 'nofollow');
externalLinkWhitelistArray.forEach(function(whiteListedUri) {
if ($linkInstance.attr('href') === whiteListedUri) {
// Get rid of rel=nofollow from whitelisted links
$linkInstance.attr('rel', '');
}
});
});
};

Which would be initialized in the views needed like so:

"use strict"
$.setAllowExternalLinksFollowed([
'http://www.google.com',
'http://www.cnn.com'
]);

All other external links will have rel="nofollow" in the DOM inspector once this script has a chance to parse and manip the DOM. The whitelisted uri's will instead have the attribute rel="nofollow" truncated to rel="" (or whatever you see fit for your project with minor adjustment).

See it in action with this pen:
http://codepen.io/nicholasabrams/pen/qdXWxB

I hope it fits the bill!

PS: If you would like to do this on the server side, you must do it in the area of your backend code where the links are generated, and do a similar comparison to what appears in the jQuery solution offered. Without seeing the backend code that renders/generates the links it would not be feasible to replicate a solution for the serverside (do the links come from an array, or are they static? We have no way of doing anything on the server if the links are static - which they are for all we know based on the OP.



Related Topics



Leave a reply



Submit