[a-Z]
doesn't mean anything, if you want uppercase and lowercase letters, two solutions:
$pattern = '/[a-z][0-9]?/i';
or
$pattern = '/[a-zA-Z][0-9]?/';
Inside a character class -
is used to define a range of characters in the unicode table. Since Z
is before a
in the table, the range doesn't exist.
Note: using [A-z]
is false too, because there are other characters than letters between Z
and a
A pattern to do that:
$formula = preg_split('/(?=[A-Z][a-z]?\d*)/', 'HgNO3', null, 1);
where (?=..)
is a lookahead and means "followed by"
And 1 is a shortcut for PREG_SPLIT_NO_EMPTY