There is a wonderful new function set in PHP 5.2+ called filter.
Filters in PHP
Excelent Tutorial on all functions and flags
It basically allows you validate like REGEXP without the confusion, general slowness, and inability to read the actual function for beginners to REGEXP.
For example in php 5.2+ i can validate an email with preg_match:
if(!preg_match("/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])([-a-z0-9_])+([a-z0-9])*(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i", $email))
{
return false;
}
return true;
or filter_var:
if(!$email = filter_var($email, FILTER_VALIDATE_EMAIL))
{
return false;
}
return true;
Much easier to read. Im doing speed testing now and will update this post with my results.
UPDATE: Benchmarked on Localhost(1.86ghz Core Duo, 1 gig ram, 5200RPM HDD)
time index ex time %
Start 1216422803.37500400 - 0.00%
Eregi Call 1216422803.37510800 0.000104 52.79%
filter_var Call 1216422803.37517300 0.000065 32.99%
Stop 1216422803.37520100 0.000028 14.21%
total - 0.000197 100.00%
On average I get a 38% performance increase, and that is only for email validation.
They also have functions for the following:
Array
(
[0] => int
[1] => boolean
[2] => float
[3] => validate_regexp
[4] => validate_url
[5] => validate_email
[6] => validate_ip
[7] => string
[8] => stripped
[9] => encoded
[10] => special_chars
[11] => unsafe_raw
[12] => email
[13] => url
[14] => number_int
[15] => number_float
[16] => magic_quotes
[17] => callback
)
Im really starting to like php5 now, 35%+ performance increases and much easier to read? Sign me up!
-Matt
