Reputation: 54856
What's the most efficient way to tell if a single character is alphanumeric in PHP?
I know I could do something like:
function isSingleAlphaNum($chr){
if(!is_string($chr){
return false;
}
//capture the first alphanumeric character, replace the whole string with it
$test = preg_replace('#([a-zA-Z0-9]{1}).*#','$1',$chr);
if($test == $chr){
return true;
}
return false;
}
This seems clunky. Is there a better way?
Upvotes: 2
Views: 92
Reputation: 4348
Good 'ol ctype_alnum() http://www.php.net/manual/en/function.ctype-alnum.php
ctype_alnum($chr);
Upvotes: 3