Categories
Code PHP

Easier Email Validation With PHP 5.2.0 or Later

OK, in my never ending quest to NOT burden the SMTP servers with unnecessary work, I’m always on the look out for a more efficient email address validation algorithm. Beginning with PHP v5.2.0 and the introduction of filter_var it’s now as easy (and as quick) as the below code example.

<?PHP
//Validate an email address in PHP 5.2.0 onwards
$email_address = "me@example";
if (filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
    // The email address is valid
    echo "validn";
} else {
    // The email address is not valid
    echo "not validn";
}
?>

The above example will output: not valid. Hope this helps, Jimmy…