preg_replace

Discussion in 'PHP' started by baris22, Jan 12, 2009.

  1. #1
    hello,

    what am i doing wrong here? it is not working

    
    
    $fullpage = preg_replace("#<td class=\"mod\">(.+?)\ </td>#i", "<td class=\"mod\">You are not allowed to view here </td>", $fullpage);
    
    
    PHP:
    thanks
     
    baris22, Jan 12, 2009 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    You specified the pattern to match against, but you didn't specify what to replace it with.
    Look at the doco for preg_replace here.
     
    phper, Jan 12, 2009 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    I match against:

    
    <td class=\"mod\"> </td>
    
    PHP:
    replace with

    
    
    <td class=\"mod\">You are not allowed to view here </td>
    
    PHP:
    is this not how to do?
     
    baris22, Jan 12, 2009 IP
  4. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Sorry I must get my eyes checked :p

    Can you confirm what you want to match? In your last post it seems like you're matching a td tag with class "mod" that contains one whitespace character. In your regex it's matching also a td tag with class "mod" that contains at least one of any character followed by one whitespace character.
     
    phper, Jan 12, 2009 IP
  5. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    I am trying to match

    
    <td class=\"mod\"> </td>
    
    PHP:
    Any td tag with mod class (there is a space just before the closing td tag)

    and replace it with

    
    <td class=\"mod\">You are not allowed to view here</td>
    
    PHP:
     
    baris22, Jan 12, 2009 IP
  6. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    In that case just remove the part with the brackets in your regex: (.+?)

    BTW, in any case, question mark after '.+' is redundant.
     
    phper, Jan 12, 2009 IP