Last active
February 13, 2016 20:54
-
-
Save romanitalian/f39ccae1dafacbc6ef83 to your computer and use it in GitHub Desktop.
php function for RFC822 - stadart php function and custom variant
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 | |
// Use: custom regex pattern | |
function validateEmail($email, $bStrict=false) { | |
$pattern = '/^("[\w-\.\s]+")|(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/i'; | |
if(preg_match($pattern, $email)) | |
return true; | |
else | |
return false; | |
} | |
// Use: filter_var (php standard function) | |
function validateEmail2($email) { | |
$email = trim($email); | |
if (strlen($email)<=0) | |
return False; | |
if (filter_var($email, FILTER_VALIDATE_EMAIL)) | |
return true; | |
return false; | |
} | |
$emails = array( | |
'[email protected]', | |
'[email protected]', | |
'[email protected]', | |
'[email protected]', | |
'[email protected]', | |
'user@[IPv6:2001:db8:1ff::a0b:dbd0]', | |
'"much.more unusual"@example.com', | |
'"[email protected]"@example.com', | |
'"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com', | |
'postbox@com', | |
'admin@mailserver1', | |
'!#$%&\'*+-/=?^_`{}|[email protected]', | |
'"()<>[]:,;@\\\"!#$%&\'*+-/=?^_`{}| ~.a"@example.org', | |
'" "@example.org', | |
'üñîçøðé@example.com', | |
'üñîçøðé@üñîçøðé.com', | |
); | |
$tmp = array(); | |
foreach($emails as $email) { | |
$tmp['custome_pattern:'][$email][] = validateEmail($email); | |
$tmp['filter_var (php standard function):'][$email][] = validateEmail2($email); | |
} | |
var_dump($tmp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment