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! Link to comment https://forums.phpfreaks.com/topic/171087-split-string-at-character/ 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)); Link to comment https://forums.phpfreaks.com/topic/171087-split-string-at-character/#findComment-902271 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). Link to comment https://forums.phpfreaks.com/topic/171087-split-string-at-character/#findComment-902621 Share on other sites More sharing options...
.josh Posted August 20, 2009 Share Posted August 20, 2009 ah good call Link to comment https://forums.phpfreaks.com/topic/171087-split-string-at-character/#findComment-902698 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.