Split alphanumeric string between leading digits and trailing letters

You can use preg_split using lookahead and lookbehind:

print_r(preg_split('#(?<=\d)(?=[a-z])#i', "0982asdlkj"));

prints

Array
(
    [0] => 0982
    [1] => asdlkj
)

This only works if the letter part really only contains letters and no digits.

Update:

Just to clarify what is going on here:

The regular expressions looks at every position and if a digit is before that position ((?<=\d)) and a letter after it ((?=[a-z])), then it matches and the string gets split at this position. The whole thing is case-insensitive (i).

Leave a Comment