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

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

问题描述

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

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

是否可以检查电子邮件地址是否有效? p>

解决方案

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

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

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

  if(!checkdnsrr($ domain,'MX')){
//域无效
}

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






现在你有了简单的答案,阅读关于电子邮件地址验证,如果你关心学习,否则只是使用快速的答案,继续前进。别往心里放。



尝试使用正则表达式验证电子邮件地址是一项不可能的任务。我会说,你所做的正则表达式是无用的。有三个rfc关于电子邮件地址和写正则表达式来捕获错误的电子邮件地址,同时没有假阳性是没有凡人可以做的。请查看此列表,以查看PHP的$ $使用的正则表达式的测试(失败和成功) c $ c> filter_var()函数。



即使内置的PHP功能,电子邮件客户端或服务器也不能正确。仍然在大多数情况下, filter_var 是最好的选择。



如果你想知道哪个正则表达式PHP(当前)用于验证电子邮件地址,请参见 PHP源



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





请注意, filter_var() code>正如PHP 5.2所述。如果您希望使用早期版本的PHP,您可以使用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-\\ ?0x7F部分))* \\x22)))* @(:((* [^] {64})(:( :( ?: XN - )?!???一个-Z0-9] +(: - + [A-Z0-9] +)* \\){1126}){1,}(:?(:[AZ] [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}(:: [一个-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}(:: [一-f0-9] {1,4}){0,3}:))))(:( ?: 25 [0-5])|(:????2 [0-4] [0-9 ])|(1:1 [0-9] {2})|(:???[1-9] [0 -9]))(?: \\(:( ?: 25 [0-5])|(:Λ2 [0-4] [0-9])|(?:?1 [O- 9] {2})|(?:?[1-9] [0-9]))){3}))\\]))$ / ID';

$ emailaddress ='test@gmail.com';

if(preg_match($ pattern,$ emailaddress)=== 1){
// emailaddress有效
}
/ pre>

PS关于上面使用的正则表达式模式的注释(来自PHP源代码)。看起来 Michael Rushton 有一些版权。如所述:请随意使用并重新分发此代码,但请保留此版权声明。


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?

解决方案

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
}

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.

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 and 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.

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.

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:

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. 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天全站免登陆