1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Regular Expression Help

Discussion in 'PHP' started by s_ruben, Apr 7, 2010.

  1. #1
    Hello

    I'd like to know how can I do this:

    For example I have a string "I like <h1>digitalpoint</h1> forum because digitalpoint forum is good.". And I want to replace "digitalpoint forum" to "this forum". How can I do it by preg_replace function for replacing both "digitalpoint forum" ignoring html tags.

    Thank you
     
    s_ruben, Apr 7, 2010 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Hi, you don't need regular expression, here is my solution:

    $s="I like <h1>digitalpoint</h1> forum because digitalpoint forum is good.";
    echo str_ireplace('digitalpoint forum','this forum',strip_tags($s))."\r\n";
    
    PHP:
    Regards :)

    Edit:OK, here is regex
    echo preg_replace('#\bdigitalpoint\s+forum\b#miu','this forum',strip_tags($s))."\r\n";
    PHP:
     
    Last edited: Apr 8, 2010
    koko5, Apr 8, 2010 IP
  3. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #3
    Thanks for your reply koko5, but I need that the function returns "I like <h1>this</h1> forum because this forum is good." not "I like this forum because this forum is good.". And I want to do it by regular expression because I need replace the string like this: '/\bdigitalpoint forum\b(?![^<]*>)/i'
     
    s_ruben, Apr 8, 2010 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    ok,maybe this:
    echo preg_replace('#(\bdigitalpoint)(\</[^\>]+\>)?(\s|&nbsp;)+(forum\b)#miu','this$2$3$4',$s)."\r\n";
    PHP:
     
    koko5, Apr 8, 2010 IP
  5. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #5
    Thank you koko5, it works. But how can I do that it replaces the "digitalpoint" to "this" if "digitalpoint" is in several tags like this: <h1><u><i>digitalpoint</i></u></h1>.

    Thank you very very much again!!!
     
    s_ruben, Apr 8, 2010 IP
  6. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #6
    You are welcome, I'm glad to help if I can :)
    Replacing (\</[^\>]+\>)? to (\</[^\>]+\>)* will do the trick:
    echo preg_replace('#(\bdigitalpoint)(\</[^\>]+\>)*(\s|&nbsp;)+(forum\b)#miu','this$2$3$4',$s)."\n";
    PHP:
    Regards :)
     
    koko5, Apr 8, 2010 IP
    s_ruben likes this.
  7. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #7
    I am sorry, but it doesn't do it. :(
     
    s_ruben, Apr 8, 2010 IP
  8. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #8
    Sorry, finally I've fixed it (hope so) :
    
    echo preg_replace('#(\bdigitalpoint)((\</[^\>]+\>)*(\s|&nbsp;)+forum\b)#miu','this$2',$s)."\n";
    
    PHP:
    :)
     
    koko5, Apr 8, 2010 IP
  9. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #9
    It works perfectly!!! Thank you very much koko5!!!
     
    s_ruben, Apr 8, 2010 IP