mbeals Posted August 6, 2008 Share Posted August 6, 2008 I have a block of text from a post variable that I need to break apart. Each 'block' will always start with a string that is nothing but numbers and it will be followed by any number of other strings adhering to 1 of 3 types. Examples work better so here's some sample data: 12345 R0012345678 S0012345678 1Z52x3a1234567 3456 R0012345622 S0012334522 Needs to broken into 12345 R0012345678 S0012345678 1Z52x3a1234567 ------------------------ 3456 R0012345622 S0012334522 I have regex that will identify each 'type' independently but I can't get a master string to work. this is what I have: <?php $wo = '\b\d+\b'; $rnum = '\b[Rr]\d+\b'; $snum = '\b[ss]\d+\b'; $track = '\b1[zZ]52[xX][\w\d]+\b'; $regex = "/($wo)[$rnum|$snum|$track]+/"; preg_match_all($regex,$text,$matches); print_r($matches); ?> It just needs to match a $wo, then any number of things matching $rnum, $snum, or $track. Quote Link to comment Share on other sites More sharing options...
effigy Posted August 6, 2008 Share Posted August 6, 2008 What kind of data structure do you need for this? An array example is below: <pre> <?php echo $data = <<<DATA 12345 R0012345678 S0012345678 1Z52x3a1234567 3456 R0012345622 S0012334522 DATA; echo '<hr>'; $pieces = preg_split('/^(\d+)(?=\s)/m', $data, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); print_r($pieces); ?> </pre> Your pattern is not working because a character class only matches a character from its pool--there are no patterns. For example, /[a|b]/ will only match "a", "|", or "b". Quote Link to comment Share on other sites More sharing options...
mbeals Posted August 6, 2008 Author Share Posted August 6, 2008 Thanks, that will work, but ideally I need it as: Array ( [0] => Array ( [0] => 12345 [1] => R0012345678 [2] => S0012345678 [3] => 1Z52x3a1234567 ) [1] => Array ( [0] => 3456 [1] => R0012345622 [2] => S0012334522 ) ) or even just ( [0] => Array ( [0] => 12345 R0012345678 S0012345678 1Z52x3a1234567 ) [1] => Array ( [0] => 3456 R0012345622 S0012334522 ) ) as I need to recursively process each chunk, but I could make the preg split option work. Quote Link to comment Share on other sites More sharing options...
effigy Posted August 6, 2008 Share Posted August 6, 2008 <pre> <?php echo $data = <<<DATA 12345 R0012345678 S0012345678 1Z52x3a1234567 3456 R0012345622 S0012334522 DATA; echo '<hr>'; $pieces = preg_split('/^(?=\d+\s)/m', $data, -1, PREG_SPLIT_NO_EMPTY); $result = array(); foreach ($pieces as $piece) { $result[] = explode("\r\n", trim($piece)); } print_r($result); ?> </pre> Quote Link to comment Share on other sites More sharing options...
mbeals Posted August 6, 2008 Author Share Posted August 6, 2008 awesome, thanks 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.