Regex help Please

Discussion in 'PHP' started by MMFBJ, Dec 13, 2011.

  1. #1
    I have the following string:
    I [like, goats] You [like cats] He [likes, dogs]

    I only need to read in the content that are between '[]' into an array.
    So the print_r would output something like this:
    Array
    (
    [0] => like, goats
    [1] => like cats
    [2] => like, dogs
    )

    Any idea how I would do this?

    Thanks!
     
    MMFBJ, Dec 13, 2011 IP
  2. proactiv3

    proactiv3 Peon

    Messages:
    55
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    0
    #2
    <?php
    $str = 'I [like, goats] You [like cats] He [likes, dogs]';
    $array = array();
    preg_match_all("/\[(.*?)\]/", $str, $array);
    print_r($array);
    PHP:
     
    proactiv3, Dec 13, 2011 IP
  3. MMFBJ

    MMFBJ Member

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Awesome! Thank you so much!!
     
    MMFBJ, Dec 13, 2011 IP