calabiyau Posted February 25, 2008 Share Posted February 25, 2008 Okay I have a data field something like below (100)+(100)++(200)===(100)++(200)++(200)== there can be any number of + or = signs between blocks within the paranthesis. I am stumped on how best to extract all the values into an array that are contained within paranthesis. so for above example array = "100,100,200,100,200,200" I know I could do an series of explodes and multiple looping array routines to get to the data but I thought there must be a regular expression solution to this. I looked into split and preg_split but can't come up with the regular expression that would represent ) plus any number of +== ( or perhaps there is someway to simply extract every value that occurs between ( ). Anybody can help with this? Quote Link to comment Share on other sites More sharing options...
fnairb Posted February 25, 2008 Share Posted February 25, 2008 If the number of numbers is vairable: $val = '(100)+(100)++(200)===(100)++(200)++(200)=='; $res = print_r(preg_split('/\D+/', $val)); However, it will give you, in this case, a null at the beginning and end of the array. If the number of numbers is fixed: preg_match(/\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)/, $val, $res); Just don't forget to get rid of $res[0] Quote Link to comment Share on other sites More sharing options...
revraz Posted February 25, 2008 Share Posted February 25, 2008 Just an FYI, there is a forum dedicated to Regex http://www.phpfreaks.com/forums/index.php/board,43.0.html Quote Link to comment Share on other sites More sharing options...
fnairb Posted February 25, 2008 Share Posted February 25, 2008 Doh!!!! Forgot the flags.... preg_split('/\D+/', $val, null, PREG_SPLIT_NO_EMPTY); will split it out and not include the empty elements. Quote Link to comment Share on other sites More sharing options...
calabiyau Posted February 25, 2008 Author Share Posted February 25, 2008 THANK YOU! Works beautifully on my limited dataset. Just so I learn something here....what exactly does the /\D+/ part do, how is this representative of the (100)+++(100), is it related to the (100) or to the )++==(, I see no indication of any of the symbols, so is it a special instruction specially for things involving parenthesis, or some kind of built in pattern seeker? Quote Link to comment Share on other sites More sharing options...
effigy Posted February 25, 2008 Share Posted February 25, 2008 \D+ matches 1 or more characters that are not a digit. Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 25, 2008 Share Posted February 25, 2008 preg_split //<< php regex function name.... ( //<<< start the regex..... ' //<<< open the container / //<<< start regex code \ \\<<<< use the charecter as it is defined D \\ a defined charecter the char d means a digit + \\< plus add another together / \\<<<< close regex container ' \\<<<< close the regex ,\\<<< adding a comma for a varable or charecter or condition $val \\ a varable , \\ add a comma for condition or varable null \\ added a condiriton of 0 but using the term NULL ,// add a comma for varable or condition PREG_SPLIT_NO_EMPTY \\use the command to make sure regex not empty )//close regex ;// tell php that this block of code stops here <?php preg_split('/\D+/', $val, null, PREG_SPLIT_NO_EMPTY); ?> Quote Link to comment Share on other sites More sharing options...
fnairb Posted February 25, 2008 Share Posted February 25, 2008 I don't mean to speak down to you so please don't take it that way. Explaining regular expressions just requires a lot of details when you don't know your audience. The regex bestiary has some very powerful escape sequences... escape meaning \d a numbers (0-9) \w a word character (0-9, a-z, A-Z, _) (and many many more) when you use the caps version of these you get an "opposite" effect escape meaning \D not a number (anything but 0-9) \W not a word characters (anything but 0-9, a-z, A-Z, _) (and many many more) Greedy Modifiers: + One or more * Zero or more ? Zero or One Take it all together /\D+/ means match one or more not number characters. If you really want to have fun with regular expressions the escape sequences will give you the power you need to solve most problems. For more see: (just search on \d to skip down to the escape sequences) http://us2.php.net/manual/en/reference.pcre.pattern.syntax.php Quote Link to comment Share on other sites More sharing options...
calabiyau Posted February 25, 2008 Author Share Posted February 25, 2008 AHHHH...okay I think I got you. So the fact that the data i'm seeking is only numbers allows me to use a very general statement to split it up by any sequence of characters that are not numbers. fiendishly simple. thanks to both of you. i will look into this alot further. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.