add rel="nofollow" to all links in a variable

Discussion in 'PHP' started by w47w47, Nov 4, 2009.

  1. #1
    title says it all.
     
    w47w47, Nov 4, 2009 IP
  2. sweetguyzzz

    sweetguyzzz Active Member

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    66
    #2
    you can use a find & replace trick like first find the "<a href=" in your variable and replace it with "<a rel='nofollow' href=".

    you can do this in php by using

    SYNTAX:
    str_ireplace("text to find","text to replace with","source text where to find the link");

    PRACTICAL:
    $no_follow = str_ireplace("<a href=","<a rel='nofollow' href=",$variable);

    Regards.
     
    sweetguyzzz, Nov 4, 2009 IP
  3. emilybarbie

    emilybarbie Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $content = preg_replace('/<a (.*?)>/s', '<a $1 rel="nofollow">', $content);
     
    emilybarbie, Nov 5, 2009 IP
  4. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    @emilybarbie this one looks good.. will take a closer look later.

    @sweetguyzzz by you, the problem is... that the nofollow should be at the end of the <a tags... also just before > and not before href= etc...

    but thank you both ;)
     
    w47w47, Nov 5, 2009 IP
  5. xd2

    xd2 Peon

    Messages:
    694
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    For the record it doesn't really matter what order tag attributes go in, so you can have the "rel" before the "href" if you want. :)
     
    xd2, Nov 8, 2009 IP
  6. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    @xd2: and what if some one adda another rel="dofollow" behind the rel="nofollow" ? when they post a comment ? :S
     
    w47w47, Nov 17, 2009 IP
  7. xd2

    xd2 Peon

    Messages:
    694
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I was stating it doesn't matter where in the tag it goes but if you was concerned about that then you should check and replace for dofollow first otherwise you would end up with duplicate attributes anyway.

    Anyway my thought didn't go that deep I just thought based on your post you had presumed that's where it had to go when in fact it doesn't really matter. But in any case I believe emilybarbie had already provided your solution as you required it.
     
    xd2, Nov 17, 2009 IP
  8. emilybarbie

    emilybarbie Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I think use <a rel="nofollow" href=....> will be better

    $content = preg_replace('/<a (.*?) href=/s', '<a rel="nofollow" $1 href=', $content);
     
    emilybarbie, Nov 17, 2009 IP
  9. snowalker

    snowalker Member

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #9
    I have a similar question, I want to add rel nofollow to a part of the links from sidebar. The widgets comes from different plugins I have installed on my wordpress blog. How can I add nofollow to that links? Thanks!
     
    snowalker, Dec 1, 2009 IP