PHP ctype_alnum to Check Alpha Numeric Characters

A function of PHP’s; ctype_alnum can be used to test a string for alpha numeric characters. It will count a space as non-alphanumeric (which is right), so you can use it like so:

$string = “test !ng”;
if ( !ctype_alnum(preg_replace(‘/[[:space:]]/’, ”, $string)) )
echo “stringy baddy”

That’s about it. Funny thing is that it takes a few Google searches for matching alpha numeric characters with php to dig this up. I wanted something prettier than regex and since I always seem to forget it, up on the blog it goes!

Post written by Ed Reckers

Founder and lead web development consultant at Red Bridge Internet : San Francisco WordPress Developers and Consultants.

One Response to “PHP ctype_alnum to Check Alpha Numeric Characters”

  1. Rajeev Jha

    You should use a str_replace instead of preg_replace

Leave a Reply