Jump to content

Split String at character


jayelge5

Recommended Posts

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

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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.