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!
Incoming search terms:
- php ctype_alnum
- ctype_alnum
- ctype_alnum spaces
- php ctype_alnum space
- ctype_alnum php
- CTYPE_alnum space
- ctype_alnum with space
- ctype_alnum and spaces
- php check alphanumeric
- count alphanumeric characters php
Related posts:

You should use a str_replace instead of preg_replace