#1
  1. 300lb Bench!
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2001
    Location
    New York
    Posts
    2,350
    Rep Power
    61

    How to get sub-matches without using preg_match_all


    Hey, guys. I have a pattern like the following:

    PHP Code:
    $ids "123,456,789,289,838"
    and would like to get all of the numbers. In other words,

    Code:
    //$matches[0] = 123
    //$matches[1] = 456
    //$matches[2] = 789
    Now I can easily do this with a preg_match_all, but wanted to be able to do it just with a preg_match. Why doesn't the following:

    PHP Code:
    preg_match("/((\d+,?)+)/"$ids$matches); 
    work? This does match the string correctly, but $matches is

    Code:
    Array
    (
        [0] => 123,456,789,289,838
        [1] => 123,456,789,289,838
        [2] => 838
    )
    I'd like 2 to be '123', 3 to be '456', etc. Thanks for any help you can give.
    Correspondence chess
    nothingbutchess.com
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    PHP Code:
    $id_array explode(','$ids); 
    Apart from that: You cannot do repeated matches with preg_match(). That's the reason why preg_match_all() even exists.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. 300lb Bench!
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2001
    Location
    New York
    Posts
    2,350
    Rep Power
    61
    Yeah, I figured. Ok, thanks.
    Correspondence chess
    nothingbutchess.com
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0
    you can use explode or preg_split.

IMN logo majestic logo threadwatch logo seochat tools logo