xProteuSx Posted December 17, 2011 Share Posted December 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253346-how-to-split-up-a-string/ Share on other sites More sharing options...
xProteuSx Posted December 17, 2011 Author Share Posted December 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253346-how-to-split-up-a-string/#findComment-1298720 Share on other sites More sharing options...
requinix Posted December 17, 2011 Share Posted December 17, 2011 Easiest way is with regular expressions. $text = "A54a1"; 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]; Quote Link to comment https://forums.phpfreaks.com/topic/253346-how-to-split-up-a-string/#findComment-1298721 Share on other sites More sharing options...
xProteuSx Posted December 17, 2011 Author Share Posted December 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253346-how-to-split-up-a-string/#findComment-1298722 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.