Jump to content

splitting a string


mrithula

Recommended Posts

The sample string is KmSO4

 

Every upper case alphabet should be identified and splitted until another upper case alphabet comes.

 

the string should be splitted so that output array looks like this

Array ( [0] => Km [1] => S [2]=>O4 )

 

can any one help regarding this issue?

 

Link to comment
Share on other sites

You could use a regular expression to look for points in the string which have a capital letter immediately following it. How familiar are you with regular expressions? (It's OK to say not at all!)

 

$array = preg_split('/(?!^)(?=[A-Z])/', 'KmSO4');

 

If that just looks like an indecipherable mess, let us know. The job can likely also be done with functions that you are more familiar with (do let us know!).

 

See also:

http://php.net/reference.pcre.pattern.syntax

http://php.net/preg_split

 

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.