Another preg match all

Discussion in 'PHP' started by goscript, Sep 7, 2008.

  1. #1
    Hi,
    What I need is to match all the texts between <!-- message --> and <!-- / message -->

    I have tried using this:

    function findinside($start, $end, $string)
    {
     preg_match_all('/' . preg_quote($start, '/') . '([^\.)]+)'. preg_quote($end, '/').'/i', $string, $m);
     return $m[1];
    }
    
    $start="<!-- message -->";
    $end="<!-- / message -->";
    $data="<!-- message -->text1<!-- / message --><!-- message -->text2<!-- / message --><!-- message -->text3<!-- / message -->";
    $out = findinside($start, $end, $data);
    print_r ($out);
    
    PHP:
    but it will only show the first result

    Later edit: seems like the problem is the / from $end but i still haven't came out with a fix
     
    goscript, Sep 7, 2008 IP
  2. goscript

    goscript Prominent Member

    Messages:
    2,753
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    315
    #2
    Also, it seems I need a multi line match.
     
    goscript, Sep 7, 2008 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    function findinside($tag, $string)
    {
        $pattern = '/<!-- '.preg_quote($tag).' -->(.*?)<!-- \/ '.preg_quote($tag).' -->/si';
        preg_match_all($pattern, $string, $m);
        return $m[1];
    }
    
    
    $tag = 'message';
    $data="<!-- message -->text1<!-- / message --><!-- message -->text2<!-- / message --><!-- message -->text3<!-- / message -->";
    $out = findinside($tag, $data);
    print_r ($out);
    PHP:
     
    JAY6390, Sep 7, 2008 IP
    goscript likes this.
  4. goscript

    goscript Prominent Member

    Messages:
    2,753
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    315
    #4
    Thanks JAY, it's working great.
     
    goscript, Sep 7, 2008 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    cool :cool:
     
    JAY6390, Sep 7, 2008 IP
  6. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #6
    /s means multi line?
     
    matthewrobertbell, Sep 7, 2008 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    JAY6390, Sep 7, 2008 IP
    matthewrobertbell likes this.
  8. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks, repped
     
    matthewrobertbell, Sep 7, 2008 IP