feha Posted April 25, 2010 Share Posted April 25, 2010 Hi, I'm collecting info of the MySQL table structure, so far im getting nice array as below: What I need help with is a regex to extract values from : [Type] => varchar(250) and get them as : [Type] => array('varchar' => '250') ... Any help appreciated, thank you in advance ... Quote Link to comment Share on other sites More sharing options...
feha Posted April 25, 2010 Author Share Posted April 25, 2010 Also: Getting as $value1 = "varchar" and $value2 ="250" is OK ... Quote Link to comment Share on other sites More sharing options...
feha Posted April 25, 2010 Author Share Posted April 25, 2010 I fixed this way: $s = 'varchar(250)'; $result = preg_split('/([0-9]+)/i', $s, -1, PREG_SPLIT_DELIM_CAPTURE); print_r($result); and getting: Array ( [0] => varchar( [1] => 250 [2] => ) ) That's not right result ... :-( Quote Link to comment Share on other sites More sharing options...
cags Posted April 25, 2010 Share Posted April 25, 2010 '#([a-z]+)\(([0-9]+))#i' Quote Link to comment Share on other sites More sharing options...
feha Posted April 25, 2010 Author Share Posted April 25, 2010 Hi cags, thank you, I'm getting an error: Compilation failed: unmatched parentheses at offset 18 don't know why ... Quote Link to comment Share on other sites More sharing options...
cags Posted April 25, 2010 Share Posted April 25, 2010 '#([a-z]+)\(([0-9]+)\)#i' Quote Link to comment Share on other sites More sharing options...
feha Posted April 25, 2010 Author Share Posted April 25, 2010 Thanks, this is better :-) $s = 'varchar(250)'; $result = preg_split('#([a-z]+)\(([0-9]+)\)#i', $s, 0, PREG_SPLIT_DELIM_CAPTURE); print_r($result); I get: Array ( [0] => [1] => varchar [2] => 250 [3] => ) btw the first 0 and last is empty ... result should be something as : Array ( [0] => varchar [1] => 250 ) or ? Quote Link to comment Share on other sites More sharing options...
cags Posted April 25, 2010 Share Posted April 25, 2010 Don't use preg_split, use preg_match and use the last parameter. Ignore [0]. preg_match('#([a-z]+)\(([0-9]+)\)#i', $input, $out); print_r($out); Quote Link to comment Share on other sites More sharing options...
feha Posted April 25, 2010 Author Share Posted April 25, 2010 Hi cag, Thank you very much, this works great :-) Quote Link to comment Share on other sites More sharing options...
feha Posted April 25, 2010 Author Share Posted April 25, 2010 Hello again ... Have still some problems ... $type = 'int'; if(preg_match('/([a-z]+)\(([0-9]+)\)/i', $type, $out)) { } else { preg_match('/([a-z]+)/i', $type, $out); } print_r($out); Is it possible to make it in one pass, regex ... as some times values are: int(10) unsigned, text date not just varchar(250) etc ... so it should match something like: txt int(10) int(10) unsigned but still to get matches ... is this possible with single regex line ? Thank you f Quote Link to comment Share on other sites More sharing options...
feha Posted April 26, 2010 Author Share Posted April 26, 2010 Hi, I still have some problems ... my current code is: $pattern = "/([a-z]+)\(([0-9]+)\)([a-z]+)|([a-z]+)\(([0-9]+)\)|([a-z]+)/i"; $type = 'int (20) unsigned'; //$type = 'int(20)'; //$type = 'text'; $type = preg_replace('/\s+/','', $type); if(preg_match($pattern, $type, $out)) { echo 1; } echo "<pre>".print_r($out,true); so far it works ok as OR ... if i try: $type = 'int(20) unsigned'; iget Array ( [0] => int(20)unsigned [1] => int [2] => 20 [3] => unsigned ) if i try $type = 'int(20)'; Array ( [0] => int(20) [1] => [2] => [3] => [4] => int [5] => 20 ) an if i try : $type = 'text'; i get: Array ( [0] => text [1] => [2] => [3] => [4] => [5] => [6] => text ) it works, but the problem is i want to use just [1],[2],[3] without having empty values ... is the possible just with RegEX ... Thank you in advance ... Quote Link to comment Share on other sites More sharing options...
feha Posted April 26, 2010 Author Share Posted April 26, 2010 I solved this by using : foreach ($out as $key => $value) { if ($out[$key] != '') { $temp[] = $value ; } } $out = $temp; But i wonder if there is other way ... 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.