jayelge5 Posted August 20, 2009 Share Posted August 20, 2009 HI, I need to split the following string examples: B6TH18K8 INTO Array ( \[0] => B6 [1] => T [2] => H18 [3] => K8) -Pls Disregard the first backslash, it was not displaying correctly without it) I need to separate the string at each non-numeric character. Another example is: S5K needs to be Array ( \[0] => S5 [1] => K) -Pls Disregard the first backslash, it was not displaying correctly without it) It could also be a delimited string such as: S5-K I cannot figure out how to do this, can someone provide code snippet to this? After much trial/error using preg_split or preg_replace and cannot figure it out, also have looked thru forum and cannot find this exact example. Any help is much appreciated. Thanks! Quote Link to comment Share on other sites More sharing options...
.josh Posted August 20, 2009 Share Posted August 20, 2009 $string = array_filter(preg_split('~([a-z](?:[0-9]+)?)~i',$string,-1,PREG_SPLIT_DELIM_CAPTURE)); Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 20, 2009 Share Posted August 20, 2009 Alternatively CV, you can include PREG_SPLIT_NO_EMPTY into the last preg_split flag instead of using array_filter: $string = preg_split('~([a-z](?:[0-9]+)?)~i',$string,-1,PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); As a bonus, your key indexes start from zero and do not skip numbers (not that this is a big deal by any stretch). Quote Link to comment Share on other sites More sharing options...
.josh Posted August 20, 2009 Share Posted August 20, 2009 ah good call 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.