Skip to content

Instantly share code, notes, and snippets.

@raveren
Last active August 29, 2015 14:14
Show Gist options
  • Save raveren/cd8a7119dee8d7695a6d to your computer and use it in GitHub Desktop.
Save raveren/cd8a7119dee8d7695a6d to your computer and use it in GitHub Desktop.
validate email
<?php
class Valid
{
/**
* Check an email address for correct format.
*
* @link http://www.iamcal.com/publish/articles/php/parsing_email/
* @link http://www.w3.org/Protocols/rfc822/
*
* @param string email address
* @param boolean strict RFC compatibility
*
* @return boolean
*/
public static function email( $email, $strict = false )
{
if ( $strict === true ) {
$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
$pair = '\\x5c[\\x00-\\x7f]';
$domain_literal = "\\x5b($dtext|$pair)*\\x5d";
$quoted_string = "\\x22($qtext|$pair)*\\x22";
$sub_domain = "($atom|$domain_literal)";
$word = "($atom|$quoted_string)";
$domain = "$sub_domain(\\x2e$sub_domain)*";
$local_part = "$word(\\x2e$word)*";
$expression = "/^$local_part\\x40$domain$/D";
} else {
$expression = '/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD';
}
return (bool) preg_match( $expression, (string) $email );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment