View Single Post
Old 02-21-2008, 02:50 AM   #3 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

A good first thing to do would be to switch from using eregi to preg_match (with the i modifier). This will improve the running time of the function to around a quarter of that of eregi! Also, this is a person preference more than anything, I don't like to see functions where a conditional is processed which determines a boolean response which is assigned to a local variable. Why not just use a single line of code? return (bool) preg_match(...);

For the regular expression itself, you've made an ok start. However, this falls victim of a hugely common bug in many "validation" checks: it would flag salathe+spam@talkphp.com as invalid, when it's not. You simply need to allow the + character in the appropriate place.

Since you're only checking to see if the pattern matches or not, there's no need to use capturing groups (...) so you can same some time and effort (on the parser's part) by making them non-capturing (?:...).
Salathe is offline  
Reply With Quote