Checking a string is alphanumeric in PHP
There may be times when you need a way to check that a string only contains alpha-numeric characters. That means only alphabets A-Z and numbers 0-9.
Most PHP-ers will probably use regular expressions and the function preg_match(). I will admit that regular expressions is really powerful, but it’s a real pain to learn and master. Fortunately PHP provides another function you can use.
Check out ctype_alnum() function.
ctype_alnum($text) will return true if $text is a mix of alphabets, numbers or both. Here’s how you can use it:
<?php // This will return true, and print out '1' $alphanum1 = "Eagle2008"; print ctype_alnum($alphanum1); // This will return false, and print out '0' $alphanum2 = "Eagle,2008"; print ctype_alnum($invalidUsername); // will return false ?>
Also, if you just want to look for purely alphabets, you can use the ctype_alpha(), and conversely, if you want to look for purely numbers in the string, you can use ctype_digit().
There is another function which does the same thing as ctype_digit, and that is is_numeric().
However, a better function to use in place of ctype_digit, might be is_numeric(). This example illustrates why:
<?php $num1 = "2008"; print ctype_digit($num1); // This will return 1 (true) print is_numeric($num1); // this will return 1 (true) $num2 = 2008; print ctype_digit($num2); // This will return 0 (false) print is_numeric($num2); // this will return 1 (true) $num3 = "-2008"; print ctype_digit($num3); // This will return 0 (false) print is_numeric($num3); // this will return 1 (true) ?>
ctype_digit purely checks for string types, whereas is_numeric takes in multiple types of variables, such as integers, floats and strings and tries to evaluate it to have a numerical value.
In: PHP Tutorials · Tagged with: php basics, sample codes, tutorial

Permalink
is_numeric and ctype_digit don’t do the same thing…
Permalink
Thanks Fredrik.
You’re right, is_numeric actually does more than ctype_digit.
ctype_digit just checks for numbers making up a string and nothing more.
is_numeric will test for integer types as well as strings, and be able to determine negative numbers.
I’ve updated the post.
Permalink
do i hav to use some header file, as none of the function wrk for me
Permalink
hi sadia
can you elaborate a bit more? are you saying your functions calls return errors?
Permalink
You’re writing about “is_numeric”, but the code uses a non-existent function called “is_number”…
Permalink
hey Remo, sharp eyes
thanks. I’ve fixed it.
Permalink
Thank you – I learned something new today!
Permalink
Seems to be an error in your script
$alphanum2 = “Eagle,2008″;
print ctype_alnum($invalidUsername); // will return false
should it not be
$alphanum2 = “Eagle,2008″;
print ctype_alnum($alphanum2); // will return false
Permalink
Hi,
I happened to see your post find it quite informative. I would like to share a link where a software engineer has shared a tip on “Alphanumeric Random value in PHP”. I am sharing it just for the knowledge purpose.
Here is the link:
http://www.mindfiresolutions.com/PHP–Alphanumeric-Random-value-262.php
Hope you find it useful and of assistance.
Thanks,
Bijayani
Permalink
Hi
Thanks for this post.
ctype_alnum function works for me….
Thanks
Permalink
Hello Every Body You can Use This Function Ereg Match OR Preg_Match With REG Expression Is Veryy Nice Than One function Because You use reg _expresionn in All Language Programation It’s My Idea ,Take A car
Youssef .
$s=”youssef125436″;
if(reg_match(“/[a-z0-9]+/”,$s))==TRUE){
return $v=true; // return true;
}
else{
return $v=false; // return false;
}
echo $v;