Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts

    Drop rel="nofollow" after any instance of <a

    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

  • #2
    Regular Coder
    Join Date
    Mar 2005
    Location
    Brighton, UK
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    strip_tags($text'<a>'); 
    That will turn "<a href="...">Jack</a>" into "Jack"

  • #3
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    $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

  • #4
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    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

  • #5
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Mar 2005
    Location
    Brighton, UK
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by GJay
    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 . This should do the job:

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

  • #7
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    Well I've gone for the nofollow option for now, but thanks again, I'll keep this bookmarked for future reference.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •