Created
January 16, 2012 12:06
-
-
Save jonas8/1620569 to your computer and use it in GitHub Desktop.
Validate an Email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * 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 | |
| */ | |
| 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