[How to] Exclude replacement inside hyperlinks, maybe using if statement,or other way

Discussion in 'PHP' started by basketmen, Apr 19, 2011.

  1. #1
    Hi guys, below are simple script for word replacement, it is replacing a word in to link



    $word = array(
    'google',
    'yahoo'
    );
    
    $link = array(
    '<a href="http://google.com">google</a>',
    '<a href="http://yahoo.com">yahoo</a>'
    );
    
    
    $this->post['message'] = str_ireplace($word, $link, $this->post['message']);
    PHP:

















    please help guys, how to exclude the text google inside hyperlinks above from replaced, but the text google outside hyperlinks are still replaced

    maybe using if statement in the simple script above, or other way
     
    basketmen, Apr 19, 2011 IP
  2. eleetgeek

    eleetgeek Peon

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    How about using preg_replace instead of str_ireplace.

    That will work :)
     
    eleetgeek, Apr 20, 2011 IP
  3. basketmen

    basketmen Well-Known Member

    Messages:
    837
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    130
    #3
    thanks for answering, but i am afraid its get error and not working, any other code or other way please?
     
    basketmen, Apr 20, 2011 IP
  4. eleetgeek

    eleetgeek Peon

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes, you are right, I just did a quick snap and it didn't work for me. I am sure, there has to be some workaround or maybe I am doing wrong.

    If you happen to not find till this weekend, I will do it, right now am at work... Weekends - Yes! I will work over it and am sure, it must a tiny thing missed :)
     
    eleetgeek, Apr 21, 2011 IP