...

View Full Version : Drop rel="nofollow" after any instance of <a



Pennimus
02-14-2006, 10:17 PM
I need to insert a link condom over any links people post into comments on my blog without my authorisation. The only way I can think of this happening is to somehow scour the comment for the precense of <a and insert rel="nofollow" before the comment gets written into the database.

Where do I need to be looking to learn how to do this, and is there a different/better way?

An alternative is simply to remove everything from opening <a to closing > and of course to also remove </a> - if this is easier I'd take this option, but it does seem more prone to failure.

-Adam

Jak-S
02-14-2006, 10:19 PM
strip_tags($text, '<a>');

That will turn "<a href="...">Jack</a>" into "Jack"

GJay
02-14-2006, 10:31 PM
$text=str_replace ('<a ','<a rel="nofollow" ',$text);

You could come up with a regex that would check for it being a link, rather than (for example) someone typing:
if( b<a )
but str_replace will be quicker, and it's too late for me to start thinking any harder :)

Pennimus
02-14-2006, 10:42 PM
Thanks guys, I will try both out. Thankfully I don't imagine anyone will type b<a at any point, but just in case at least the strip_tags method seems pretty fullproof :)

GJay
02-14-2006, 10:53 PM
I only just actually read the post above mine properly, and putting <a> as the second argument means that all tags except <a> will be stripped
If you take a look at the manual page for strip-tags: http://php.net/strip-tags then the top comment has a function to do the opposite.

Jak-S
02-14-2006, 10:58 PM
I only just actually read the post above mine properly, and putting <a> as the second argument means that all tags except <a> will be stripped
If you take a look at the manual page for strip-tags: http://php.net/strip-tags then the top comment has a function to do the opposite.

Oops, your totally right, my mistake :o . This should do the job:



$test = preg_replace('/<a[^>]*>(.*?)<\/a>/is', '$1', $test);

Pennimus
02-15-2006, 09:05 AM
Well I've gone for the nofollow option for now, but thanks again, I'll keep this bookmarked for future reference.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum