PHP5 Email Validation

In prior version of PHP you had to use some regex solution to validate an email address, now in PHP5 it’s as simple as:

function isValidEmail($email){
 return filter_var(filter_var($email, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);

if(isValidEmail('[email protected]')){
} else {
//not valid

I’ve been using PHP5 ever since it was released but didn’t look into the PECL Filter extension until recently. Pretty handy.

Please note that I also added the FILTER_SANITIZE_EMAIL filter which removes all illegal e-mail characters from a string. This is not needed if you only want to validate and not clean the input. 🙂