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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.