Home > Article > Backend Development > PHP Regular Method to Verify Email_PHP Tutorial
This article describes the method of PHP regular verification of Email. Share it with everyone for your reference. The details are as follows:
ใ?
|
<๐>function validateEmail($email)<๐> <๐>{<๐> <๐>$isValid = true;<๐> <๐>$atIndex = strrpos($email, "@");<๐> <๐>if (is_bool($atIndex) && !$atIndex)<๐> <๐>{<๐> <๐>$isValid = false;<๐> <๐>}<๐> <๐>else<๐> <๐>{<๐> <๐>$domain = substr($email, $atIndex 1);<๐> <๐>$local = substr($email, 0, $atIndex);<๐> <๐>$localLen = strlen($local);<๐> <๐>$domainLen = strlen($domain);<๐> <๐>if ($localLen < 1 || $localLen > 64) { // local part length exceeded $isValid = false; } else if ($domainLen < 1 || $domainLen > 255) { // domain part length exceeded $isValid = false; } else if ($local[0] == '.' || $local[$localLen-1] == '.') { // local part starts or ends with '.' $isValid = false; } else if (preg_match('/\.\./', $local)) { // local part has two consecutive dots $isValid = false; } else if (!preg_match('/^[A-Za-z0-9\-\.] $/', $domain)) { // character not valid in domain part $isValid = false; } else if (preg_match('/\.\./', $domain)) { // domain part has two consecutive dots $isValid = false; } else if(!preg_match('/^(\\.|[A-Za-z0-9!#%&`_=\/$'* ?^{}|~.-]) $/' , str_replace("\\","",$local))) { // character not valid in local part unless // local part is quoted if (!preg_match('/^"(\\"|[^"]) "$/', str_replace("\\","",$local))) { $isValid = false; } } if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { // domain not found in DNS $isValid = false; } } return $isValid; } ?> |
I hope this article will be helpful to everyoneโs PHP programming design.