Validation with CType

By | | PHP

What happens when you take you eye off the ball? The CType extension becomes a default part of PHP!

Have to confess I’ve never touched them until recently and was stuck in a “validation = regular expressions” mindset. What puzzles me is I haven’t seen anyone else using them either – most libraries doing some validation, including QuickForm and Validate all use regular expressions. Anyone know why? It’s not hard to use them but fall back to regular expressions for older PHP versions.

The CType functions are wrappers for ctype.h, a standard part of C, so are, effectively, very well tested.

The point is, for validating certain basic types of data, they do a better job than regular expressions, being both faster and supporting international character sets. The also make your code more “declarative” in nature – it’s pretty clear what ctype_digit() does while ‘/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/’ is less obvious.

For example say you have a field in your form for “First Name”. Using regular expressions, you’d probably validate it something like;

if ( !preg_match('/^[a-zA-Z]*$/', $first_name) ) { die ('Alpha characters only please!'); }

Now that’s fine if your name is Joe but what if your name is H

Written By:

Harry Fuecks

Harry has been working in corporate IT since 1994, with everything from start-ups to Fortune 100 companies. Outside of office hours he runs phpPatterns: a site dedicated to software design with PHP that aims to raise standards of PHP development. He also maintains Dynamically Typed: SitePoint's PHP blog.

 

{ 12 comments }

Widow Maker May 27, 2004 at 3:23 pm

Knew of it but didn’t know what it did ;)

But I’d still proberly use Reg Exp anyways, just out of habit if nothing more :lol:

NativeMind May 22, 2004 at 1:07 am

no multibyte applications? man, that’s rough — come on folks, multibyte is essential to a successful API

nucleuz May 13, 2004 at 4:33 am

Tested this out on OSX 10.3.2 ( Norwegian edition ) and as the previous comment says: this doesn’t work with H

Tim Strehle May 13, 2004 at 3:35 am

“supporting international character sets” – well, yes, but I’ve done some investigation and it seems to me that ctype does not support multibyte character encodings.

So for UTF-8 applications, you cannot use ctype_alpha() and the like…

HarryF May 12, 2004 at 8:48 pm

I get “Alpha characters only please!”

I guess that’s the locale settings your OS is running. You probably want to explore setlocale() to set the country to somewhere like Norway. I need to do some more research on setlocale and all the ins and outs on Linux vs Windows. Will blog it some time.

Nola May 12, 2004 at 1:55 pm

I used this:


$first_name = "H

khlo May 12, 2004 at 11:39 am

Good find, seems to be on the PHP index at http://www.php.net/manual/en/ now “XV. Character Type Functions”. Will be trying later, never been fan of regexp

Dangermouse May 12, 2004 at 10:11 am

Wow, i never knew about ctype, thanks!

nucleuz May 12, 2004 at 8:11 am

A very good thing to have indeed when doing a multi-language application :) Been around for a while though ( a 4.3.2 version has it by default here ) so it’s a bit weird to see that it isn’t in the main docs.

HarryF May 12, 2004 at 6:10 am

Hmmm – perhaps part of the reason no ones using these is because they don’t exist, according to the contents page of the manual: http://www.php.net/manual/en/ . The do turn up in the function index though: http://www.php.net/manual/en/indexes.php.

eXplosive May 12, 2004 at 12:34 am

I have never heard of these functions. Nice find!

Darren884 May 11, 2004 at 10:13 pm

Wow thats a good tip! Thanks!

Comments on this entry are closed.