#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Posts
    42
    Rep Power
    8

    Fairly simple PHP regular expression question


    I've never been great with regular expressions, and I'm at a total lose right now. I have a fairly (very?) simple task but I'm very bad with these, so I would appreciate some help. I have a string that has several blocks of text enclosed in brackets, for instance, the string may look like this:
    Code:
    [Hello][World][I'm][having][some][MAJOR PROBLEMS]
    and I need to get each of those blocks of text into an array so $array[0] = "Hello", $array[1] = "World", etc. Can anyone help? Thanks!
  2. #2
  3. Expert Debugger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2006
    Location
    Dev Shed Forums (-_^)v
    Posts
    1,022
    Rep Power
    1302
    kaboom !

    PHP Code:
    <?php
    $str
    ="[Hello][World][I'm][having][some][MAJOR PROBLEMS]";
    $pt="#\[(.+?)\]#i";
    $ret=preg_match_all($pt,$str,$matches);

    print_r($matches[1]);
    ?>

    Comments on this post

    • magician13134 agrees : Ah! Thanks
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Posts
    42
    Rep Power
    8
    Ok. Thanks. I've never used preg_match_all, I'll have to look into that. Also are the # at the beginning and end identical to using a / ? Thanks
  6. #4
  7. Expert Debugger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2006
    Location
    Dev Shed Forums (-_^)v
    Posts
    1,022
    Rep Power
    1302
    Originally Posted by magician13134
    Ok. Thanks. I've never used preg_match_all, I'll have to look into that. Also are the # at the beginning and end identical to using a / ? Thanks
    yes

IMN logo majestic logo threadwatch logo seochat tools logo