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.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • Propeller
  • Reddit
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
Posted on April 11, 2008 at 4:18 pm by Eldee · Permalink
In: PHP Tutorials · Tagged with: , ,

11 Responses

Subscribe to comments via RSS

  1. Written by Fredrik Holmström on April 18, 2008 at 9:41 pm
    Permalink

    is_numeric and ctype_digit don’t do the same thing…

  2. Written by webmaster on April 21, 2008 at 10:31 am
    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.

  3. Written by sadia on February 6, 2009 at 7:23 pm
    Permalink

    do i hav to use some header file, as none of the function wrk for me

  4. Written by webmaster on February 9, 2009 at 8:03 am
    Permalink

    hi sadia
    can you elaborate a bit more? are you saying your functions calls return errors?

  5. Written by Remo Laubacher on May 16, 2009 at 4:19 pm
    Permalink

    You’re writing about “is_numeric”, but the code uses a non-existent function called “is_number”…

  6. Written by webmaster on May 18, 2009 at 10:29 am
    Permalink

    hey Remo, sharp eyes :) thanks. I’ve fixed it.

  7. Written by Shimon on August 12, 2009 at 1:15 am
    Permalink

    Thank you – I learned something new today!

  8. Written by Duncan on January 23, 2010 at 12:00 am
    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

  9. Written by Bijayani on February 2, 2010 at 9:18 pm
    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

  10. Written by Avi on September 1, 2010 at 8:20 pm
    Permalink

    Hi
    Thanks for this post.
    ctype_alnum function works for me….

    Thanks

  11. Written by youssef on September 19, 2010 at 2:04 am
    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;

Subscribe to comments via RSS

Leave a Reply