Jump to content

How to Split up a String


xProteuSx

Recommended Posts

I've been trying to figure out how to split up strings such as the following:

 

A54a1, 23b, FX49s1, R231z, 77

 

I would like to split them up and add them to individual arrays that are as follows:

 

index[0] -> alphabetical characters, if any, prior to the first number, if any

index[1] -> numerical characters, as a number, up until the following alphabetical character

index[2] -> alphabetical character(s), if any

index[3] -> numerical characters following the second alphabetical character set, if any

 

So, for example, the above strings would be separated like this:

A54a1 == ('A', 54, 'a', 1)

23b == (, 23, 'b',,)

FX49s1 == ('FX', 49, 's', 1)

R231z == ('R', 231, 'z',,)

77 == (,77,,)

 

I've looked into things like strpos(), but I can't figure how to determine whether a character is alphabetic or numeric.  And yes, I am familiar with the function is_numeric().  Just don't know how to throw it all together to accomplish what I need.

Link to comment
Share on other sites

Guess I should mention some rules:

 

If there are alphabetical characters at the beginning, there may be 1 or 2, if any at all.

There must be a number in the string, which is 1-4 digits long.

This number may or may not be followed by one or two alphabetical characters.

If the first number is followed by an alphabetical character or two, this may be followed with another number that is either 1 or 2 digits in length.

Link to comment
Share on other sites

Thank you very much requinix!

 

Well, I had to change things a little bit, because when I output the $matches array, it showed the full text at index 0.  Just used array_shift to get rid of this index.  What I've got now is as follows:

 

$text = "A54sd12";

preg_match('/^([a-z]*)(\d+)([a-z]*)(\d*)$/i', $text, $matches);

 

$index[0][] = $matches[1];

$index[1][] = $matches[2];

$index[2][] = $matches[3];

$index[3][] = $matches[4];

 

$note = array_shift($matches);

print_r($matches);

 

So, to try out all of the different strings:

 

$strings = array('A54a1', '23b', 'FX49s1', 'R231z', '77');

 

foreach ($strings as $string)

{

preg_match('/^([a-z]*)(\d+)([a-z]*)(\d*)$/i', $string, $matches);

 

$index[0][] = $matches[1];

$index[1][] = $matches[2];

$index[2][] = $matches[3];

$index[3][] = $matches[4];

 

$note = array_shift($matches);

print_r($matches);

echo '<br />';

}

 

Output is perfect:

 

Array ( [0] => A [1] => 54 [2] => a [3] => 1 )

Array ( [0] => [1] => 23 [2] => b [3] => )

Array ( [0] => FX [1] => 49 [2] => s [3] => 1 )

Array ( [0] => R [1] => 231 [2] => z [3] => )

Array ( [0] => [1] => 77 [2] => [3] => )

 

Thanks again.

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.