How to make a preg_match in PHP for a string of 2 characters, for example:
1a valid
b2 valid
3c valid
11 invalid
67 invalid
ad invalid
dt invalid
I have tried this but it doesn't work:
$string = "aa";
$result = preg_match('/^([a-z]|[0-9]){2}$/i', $string);
It matches everything instead of matching only if the first char is a letter and then a number and vice versa.
preg_match('/^([a-z][0-9]|[0-9][a-z])$/i', $string);
Less readable than Rolando's answer, but slightly shorter
^[\d|[a-z][\d|[a-z]$
See it in action here: https://regex101.com/r/dXCDcb/4