cwd
cwd

Reputation: 54856

What's the most efficient way to tell if a single character is alphanumeric in PHP?

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

Answers (2)

aknosis
aknosis

Reputation: 4348

Good 'ol ctype_alnum() http://www.php.net/manual/en/function.ctype-alnum.php

ctype_alnum($chr); 

Upvotes: 3

Alexander Gessler
Alexander Gessler

Reputation: 46677

What about ctype-alnum?

Upvotes: 2

Related Questions