Take the 2-minute tour ×

Is there a Php function to determine if a string consist of only ASCII alphanumerical characters?

Note: I'm sorry if the question sounds dumb to some, but I couldn't easily find such a function in the Php manual.

share|improve this question
add comment

3 Answers

up vote 8 down vote accepted

Try ctype_alnum

share|improve this answer
Exactly what I was looking for, thanks! –  Robert Gould Dec 1 '08 at 7:38
add comment
preg_match('/^[a-z0-9]+$/i', $str);

Edit: John T's answer is better. Just providing another method.

share|improve this answer
Thanks anyways, this is a somewhat reasonable alternative. –  Robert Gould Dec 1 '08 at 7:37
add comment

This is my solution

public function alphanum($string){
        $return = ctype_alnum($string);
        $return = preg_match('/^[a-z0-9]+$/i', $string) > 0;
    return $return;
share|improve this answer
add comment

Your Answer


