grab content

Discussion in 'PHP' started by playwright, Jul 14, 2010.

  1. #1
    Hello!! i want to grab all content that is between the tags <-- message --> and <!-- message -->.. I have found the right pattern to do so which is something like preg_match_all('%<!-- message -->(.+)<!-- / message -->%si',$x,$y) but it seems that it grabs all content from the 1st tag <-- message --> untill the last tag <!-- message --> in one record. However, there are many more such tags between the first and the last encounter and the thing is that i want to fill my array with all this records separately..Any ideas???
     
    playwright, Jul 14, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    .+ is greedy add ? to make it ungreedy (or use the U modifier),

    preg_match_all('~<!\-\- message \-\->(.+?)<!\-\- / message \-\->~si', $x, $y);
    PHP:
    Also escape/backslash the - character as its considered a special character within regex.
     
    danx10, Jul 14, 2010 IP
  3. playwright

    playwright Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I added ? and it works fine..Thanks for the help!!
     
    playwright, Jul 14, 2010 IP