如何在 PHP 中验证电子邮件地址 [英] How to validate an email address in PHP

查看:25
本文介绍了如何在 PHP 中验证电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能来验证电子邮件地址:

I have this function to validate an email addresses:

function validateEMAIL($EMAIL) {
    $v = "/[a-zA-Z0-9_-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/";

    return (bool)preg_match($v, $EMAIL);
}

这样可以检查电子邮件地址是否有效吗?

Is this okay for checking if the email address is valid or not?

推荐答案

检查电子邮件地址是否格式正确的最简单、最安全的方法是使用 filter_var() 函数:

The easiest and safest way to check whether an email address is well-formed is to use the filter_var() function:

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

此外,您可以检查域是否定义了 MX 记录:

Additionally you can check whether the domain defines an MX record:

if (!checkdnsrr($domain, 'MX')) {
    // domain is not valid
}

但这仍然不能保证邮件存在.唯一的方法是发送确认邮件.

But this still doesn't guarantee that the mail exists. The only way to find that out is by sending a confirmation mail.

现在您有了简单的答案,如果您想学习,请随时阅读有关电子邮件地址验证的内容,或者只是使用快速答案并继续.没有难受的感觉.

Now that you have your easy answer feel free to read on about email address validation if you care to learn or otherwise just use the fast answer and move on. No hard feelings.

尝试使用正则表达式验证电子邮件地址是不可能的"任务.我什至会说您制作的正则表达式毫无用处.有三个关于电子邮件地址和编写正则表达式以捕获错误电子邮件地址的 rfc,同时没有误报是凡人无法做到的.查看此列表,了解正则表达式的测试(失败和成功)由 PHP 的 filter_var() 函数使用.

Trying to validate an email address using a regex is an "impossible" task. I would go as far as to say that that regex you have made is useless. There are three rfc's regarding emailaddresses and writing a regex to catch wrong emailadresses and at the same time don't have false positives is something no mortal can do. Check out this list for tests (both failed and succeeded) of the regex used by PHP's filter_var() function.

即使是内置的 PHP 函数、电子邮件客户端或服务器也无法正确处理.仍然在大多数情况下 filter_var 是最好的选择.

Even the built-in PHP functions, email clients or servers don't get it right. Still in most cases filter_var is the best option.

如果您想知道 PHP(当前)使用哪种正则表达式模式来验证电子邮件地址,请参阅 PHP 源代码.

If you want to know which regex pattern PHP (currently) uses to validate email addresses see the PHP source.

如果您想了解有关电子邮件地址的更多信息,我建议您开始阅读规范,但我必须警告您,无论如何都不容易阅读:

If you want to learn more about email addresses I suggest you to start reading the specs, but I have to warn you it is not an easy read by any stretch:

  • rfc5322
  • rfc5321
  • rfc3696
  • rfc6531 (allows unicode characters, although many clients / servers don't accept it)

请注意,filter_var() 如前所述,仅在 PHP 5.2 中可用.如果您希望它与早期版本的 PHP 一起使用,您可以使用 PHP 中使用的正则表达式:

Note that filter_var() is as already stated only available as of PHP 5.2. In case you want it to work with earlier versions of PHP you could use the regex used in PHP:

<?php

$pattern = '/^(?!(?:(?:\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';

$emailaddress = 'test@gmail.com';

if (preg_match($pattern, $emailaddress) === 1) {
    // emailaddress is valid
}

P.S.关于上面使用的正则表达式模式的注释(来自 PHP 源代码).看起来 Michael Rushton.如前所述:请随意使用和重新分发此代码.但请保留此版权声明."

P.S. A note on the regex pattern used above (from the PHP source). It looks like there is some copyright on it of Michael Rushton. As stated: "Feel free to use and redistribute this code. But please keep this copyright notice."

这篇关于如何在 PHP 中验证电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆