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

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.


Try ctype_alnum

preg_match('/^[a-z0-9]+$/i', $str);

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

This is my solution

public function alphanum($string){
        $return = ctype_alnum($string);
        $return = preg_match('/^[a-z0-9]+$/i', $string) > 0;
    return $return;

My long, but simple solution

strspn($string, '0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_') == strlen($string)

Function strspn() finds the length of the initial segment of $string that contains only letters, numbers and underscore character (second argument). If entire string consists only of letters, numbers and underscore, the returned value will be equeal to the length of the string.

Need Your Help

onComplete for Ajax.Request buggy with cached content

javascript ruby-on-rails ruby prototypejs

I have a Ajax.Request in which I use onLoading and onComplete to show and hide a load animation gif. The problem is that every 10 clicks or so, the load animation fails to hide and just stays there

PHP root folder

php file root

rename('/images/old_name.jpg', '/images/new_name.jpg');