在 PHP 中使用正则表达式进行电子邮件验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13719821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:57:22  来源:igfitidea点击:

Email validation using regular expression in PHP

phpregexemail

提问by Wayne Whitty

I am pretty much new with regular expression. I am developing a project in PHP and i need to validate email address. After searching in this site and google i found the following regular expression says it should work best.

我对正则表达式很陌生。我正在用 PHP 开发一个项目,我需要验证电子邮件地址。在这个网站和谷歌搜索后,我发现下面的正则表达式说它应该工作得最好。

if (preg_match("/^(?!(?:(?:\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}))\]))$/iD", strtolower($mail_address))) {
    echo "valid email";
}

But when I use it I get error says,

但是当我使用它时,我收到错误消息,

"Warning: preg_match(): No ending delimiter '/' found"

What is wrong with this code?

这段代码有什么问题?

回答by Wayne Whitty

Use this instead of a regular expression:

使用它而不是正则表达式:

if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
     //Valid email!
}

Using regular expressions to validate email addresses is not recommendedas it is certainly not pretty, especially if you don't want to exclude somebody who has a valid email address that is correct according to RFC 822 grammar.

不建议使用正则表达式来验证电子邮件地址,因为它肯定不美观,特别是如果您不想排除具有根据 RFC 822 语法正确的有效电子邮件地址的人。

http://www.php.net/filter_varis your best bet.

http://www.php.net/filter_var是您最好的选择。

A crazy example of a regex that attempts to validate email addresses according to RFC 822 grammar:

尝试根据 RFC 822 语法验证电子邮件地址的正则表达式的疯狂示例:

(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\".\[\] 
filter_var('[email protected]', FILTER_VALIDATE_EMAIL)
0-1]+(?:(?:(?:\r\n)?[ \t] )+|\Z|(?=[\["()<>@,;:\".\[\]]))|"(?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t]))*"(?:(?: \r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\]
<?php
$email = "[email protected]"; 
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; 
if (preg_match($regex, $email)) {
 echo $email . " is a valid email. We can accept it.";
} else { 
 echo $email . " is an invalid email. Please try again.";
}           
?>
0-1]+(?:(?:( ?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|"(?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\]
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/  
0-
 filter_var($email, FILTER_VALIDATE_EMAIL)
31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|\[([^\[\]\r\]|\.)*\ ](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\]
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
     //yup its valid email
}
0-1]+ (?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|\[([^\[\]\r\]|\.)*\](?: (?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\".\[\]
function isValidEmail($email)
{
    return preg_match('/\A[a-z0-9]+([-._][a-z0-9]+)*@([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,4}\z/', $email)
        && preg_match('/^(?=.{1,64}@.{4,64}$)(?=.{6,100}$).*/', $email);
}
0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z |(?=[\["()<>@,;:\".\[\]]))|"(?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n) ?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\".\[\]
function validate($email) {
    return preg_match("/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/", $email);    
}
0-1]+(?:(?:(?:\ r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\]
function validate($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) {
            $isValid = false;
        } else if ($domainLen < 1 || $domainLen > 255) {
            $isValid = false;
        } else if ($local[0] == '.' || $local[$localLen - 1] == '.') {
            $isValid = false;
        } else if (preg_match('/\.\./', $local)) {
            $isValid = false;
        } else if (!preg_match('/^[A-Za-z0-9\-\.]+$/', $domain)) {
            $isValid = false;
        } else if (preg_match('/\.\./', $domain)) {
            $isValid = false;
        } else if (!preg_match('/^(\\.|[A-Za-z0-9!#%&`_=\/$\'*+?^{}|~.-])+$/', str_replace("\\", "", $local))) {
            if (!preg_match('/^"(\\"|[^"])+"$/', str_replace("\\", "", $local))) {
                $isValid = false;
            }
        }

        if ($isValid && !(checkdnsrr($domain, "MX") || checkdnsrr($domain ,"A"))) {
            $isValid = false;
        }
    }

    return $isValid;
}
0-1]+(?:(?:(?:\r\n) ?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t] )*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\]
^((([a-z0-9]+[\-]{0,1})+[a-z0-9]+?\.)+[a-z0-9][a-z0-9|\-]+[a-z0-9])@([a-z0-9]+[\-|\.]{0,1}[a-z0-9]+)+(.[a-z0-9]{2,})+$
0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])* )(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\]
^([a-z0-9]+[\.|\-]{0,1}[a-z0-9](.[a-z0-9]{2,})+)$
0-1]+(?:(?:(?:\r\n)?[ \t] )+|\Z|(?=[\["()<>@,;:\".\[\]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*))*) *:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\".\[\]
^([a-z0-9]+[\.|\-]{0,1}[a-z0-9]{1,})+@$
0-1]+(?:(?:(?:\r\n)?[ \t])+ |\Z|(?=[\["()<>@,;:\".\[\]]))|"(?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r \n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?: \r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|"(?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t ]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\] ##代码##0-1 ]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|\[([^\[\]\r\]|\.)*\]( ?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\] ##代码##0-1]+(? :(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|\[([^\[\]\r\]|\.)*\](?:(? :\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)|(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(? :(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|"(?:[^\"\r\]|\.|(?:(?:\r\n)? [ \t]))*"(?:(?:\r\n)?[ \t])*)*:(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|"(?:[^\"\r\]| \.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<> @,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|" (?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t] )*(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\ ".\[\]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(? :[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[ \]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\".\[\] ##代码##0- 1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|"(?:[^\"\r\]|\.|( ?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,; :\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|\[([ ^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\" .\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|\[([^\[\ ]\r\]|\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\ [\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|\[([^\[\]\ r\]|\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|\[([^\[\]\r\] |\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\".\[\] ##代码## 00-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|"(?:[^\"\r\]|\ .|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@, ;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\]]))|"(? :[^\"\r\]|\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])* (?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\". \[\]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[ ^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\] ]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)(?:,\s*( ?:(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\ ".\[\]]))|"(?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:( ?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[ \["()<>@,;:\".\[\]]))|"(?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t ])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t ])+|\Z|(?=[\["()<>@,;:\".\[\]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*)(? :\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+| \Z|(?=[\["()<>@,;:\".\[\]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*))*|(?: [^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\".\[\ ]]))|"(?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n) ?[ \t])*(?:@(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[" ()<>@,;:\".\[\]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n) ?[ \t])*(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<> @,;:\".\[\]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@, ;:\".\[\]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t] )*(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\ ".\[\]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)? (?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\". \[\]]))|"(?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?: \r\n)?[ \t])*(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[ "()<>@,;:\".\[\]]))|"(?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t]) *))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t]) +|\Z|(?=[\["()<>@,;:\".\[\]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*)(?:\ .(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\".\[\] ##代码##0-1]+(?:(?:(?:\r\n)?[ \t])+|\Z |(?=[\["()<>@,;:\".\[\]]))|\[([^\[\]\r\]|\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:( ?:\r\n)?[ \t])*))*)?;\s*)

回答by koopajah

You should use the function filter_vars()to validate your input instead of your own regexp.

您应该使用该函数filter_vars()来验证您的输入,而不是您自己的正则表达式。

Example:

例子:

##代码##

More info in the documentation : http://php.net/manual/en/function.filter-var.php

文档中的更多信息:http: //php.net/manual/en/function.filter-var.php

回答by Kuldeep Singh

it looks like you don't really need that bigger code to just validate a email.

看起来您真的不需要那么大的代码来验证电子邮件。

##代码##

Try this one and let me know

试试这个,让我知道

回答by user007

try this regx it would work fine

试试这个 regx 它会工作得很好

##代码##

or php filter filter_var

或 php 过滤器 filter_var

##代码##

like

喜欢

##代码##

回答by Alptugay

Remove the iDat the end of the regex

删除iD正则表达式末尾的

回答by Mehdi Bugnard

Weird I just tried your code that works well with me ?! Try closing any chain with the character '....' instead of "...."

奇怪,我刚刚试过你的代码对我很有效?!尝试用字符“....”而不是“....”来关闭任何链

回答by viniciusbo

I got here looking for a solution and the answer presented by Wayne Whitty do the job, indeed.

我来到这里寻找解决方案,Wayne Whitty 提出的答案确实可以胜任。

BTW, I think it is good to mention that filter_varfunction is not RFC5321 compliant. Altought the docs does not say it is, some RFC5321 valid email address will be flagged as invalid by this function, like:

顺便说一句,我认为最好提及该filter_var功能不符合 RFC5321。尽管文档没有说明,但此功能将一些 RFC5321 有效电子邮件地址标记为无效,例如:

  • [email protected]
  • (comment)[email protected]
  • "this is v@lid!"@example.com
  • "much.more unusual"@example.com
  • postbox@com
  • admin@mailserver1
  • "()<>[]:,;@\"\!#$%&'*+-/=?^_`{}| ~.a"@example.org
  • " "@example.org
  • [email protected]
  • (评论)[email protected]
  • “这是 v@lid!”@example.com
  • “更不寻常”@example.com
  • 邮箱@com
  • 管理员@邮件服务器1
  • "()<>[]:,;@\"\!#$%&'*+-/=?^_`{}| ~.a"@example.org
  • " "@example.org

As presented by this comment http://php.net/manual/pt_BR/function.filter-var.php#112492

正如此评论所呈现的http://php.net/manual/pt_BR/function.filter-var.php#112492

回答by Geek

Use these regular expressions to perform a more thorough validation. For example it prevents adjacent periods in email address like [email protected] which is invalid according to RFC 822.

使用这些正则表达式来执行更彻底的验证。例如,它可以防止电子邮件地址中的相邻句点,如 [email protected],根据 RFC 822 无效。

##代码##

See validate email address using regular expression in PHP.

请参阅在 PHP 中使用正则表达式验证电子邮件地址

回答by Vishnu

There more than one ways to check if an e-mail is valid.

检查电子邮件是否有效的方法不止一种。

One simple way is to use this function:

一种简单的方法是使用这个函数:

##代码##

It is from a code snippet found here: Basic Email Validation in PHP

它来自此处的代码片段:Basic Email Validation in PHP

Although this implementation would work most of the time it may allow some wrong emails. The following implementation ahould be 100% accurate but is also quite complex:

尽管此实现在大多数情况下都可以工作,但它可能会允许一些错误的电子邮件。以下实现应该是 100% 准确的,但也相当复杂:

##代码##

This code is from snippet found here: Strict Email Validation in PHP

此代码来自此处的片段:Strict Email Validation in PHP

Hope this helps!

希望这可以帮助!

回答by BinX

Email validation regex (domain with sub domains)

电子邮件验证正则表达式(带有子域的域)

##代码##

Sample tests https://regex101.com/r/w5VBdf/20

示例测试https://regex101.com/r/w5VBdf/20

Domain with sub domains

带有子域的域

##代码##

https://regex101.com/r/4Un8Tz/2

https://regex101.com/r/4Un8Tz/2

Email username

电子邮件用户名

##代码##

https://regex101.com/r/4Un8Tz/6

https://regex101.com/r/4Un8Tz/6