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!
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Mar | ||||||
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 | |
One Response
Rajeev Jha
February 19th, 2010 at 6:36 am
1You should use a str_replace instead of preg_replace
RSS feed for comments on this post · TrackBack URI
Leave a reply