PHP电子邮件地址验证问题

发布于 2024-09-25 19:34:28 字数 182 浏览 5 评论 0原文

我想知道在输入框中输入电子邮件地址时如何检查是否包含 @ 符号?我正在使用 PHP。

这是我的 php 代码。

if (isset($_POST['email']) && strlen($_POST['email']) <= 255)

I was wondering how can I just check if an @ sign has been included when an email address is entered into the input box? I'm using PHP.

Here is my php code.

if (isset($_POST['email']) && strlen($_POST['email']) <= 255)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

漫雪独思 2024-10-02 19:34:29

这就是我用的。工作完美。

public function valid_mail($mail)
{
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $mail))
{
return false;
} else {
return true;
}
}

This is what I use. Works perfectly.

public function valid_mail($mail)
{
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $mail))
{
return false;
} else {
return true;
}
}
嗳卜坏 2024-10-02 19:34:28

可以只使用一个简单的:

filter_var($email, FILTER_VALIDATE_EMAIL)

如果您绝对必须使用正则表达式,那么我会推荐(这表示正则表达式不应该用于验证电子邮件地址):

"/^(?!(?:(?:\\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"

Could just use a simple:

filter_var($email, FILTER_VALIDATE_EMAIL)

If you absolutely must use a Regex, than I would recommend (This is to signify that regex should NOT be used to validate email addresses):

"/^(?!(?:(?:\\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"
留一抹残留的笑 2024-10-02 19:34:28
if(strstr($email,"@"))
{
// true
}

确定电子邮件是否正常的更好方法是

\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b 

在 preg_match() 中使用此正则表达式

if(strstr($email,"@"))
{
// true
}

A better way to find if the email is fine is by using this regex

\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b 

in preg_match()

遮了一弯 2024-10-02 19:34:28

您可能希望使用 preg 而不是松散的 strpos($_POST['email'],"@") 进行检查:

if (preg_match($_POST["email"],"/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i")) {
// do stuff
}

来源

You might want to have this checked using a preg instead of a loose strpos($_POST['email'],"@"):

if (preg_match($_POST["email"],"/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i")) {
// do stuff
}

Source

萌梦深 2024-10-02 19:34:28

只是引用 strstr 上的文档。 “如果您只想确定某个特定的针是否出现在 haystack 中,请使用速度更快、内存占用更少的函数 strpos()。”

关于 strpos 我注意到的另一件事是,仅检查 strpos 的真实性是不安全的,因为如果 $needle 位于 $haystack 中的第 0 个位置,则简单的检查将失败。您还必须检查它的类型(至少我这样做)。这将打印“未找到”。

<?php

$str = 'foobar';

if (strpos($str, 'foo')) {
    echo 'found ';
} else {
    echo 'notfound ';
}

// proper...
if (strpos($str, 'foo') !== false) {
    echo 'found ';
} else {
    echo 'notfound ';
}

Just a quote from the docs on strstr. "If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead."

Another thing on strpos I've noticed is that it's unsafe to just check for truth of strpos because if $needle is at 0'th position in $haystack, a simple check will fail. You must check it's type as well (at least I do). This will print "notfound found".

<?php

$str = 'foobar';

if (strpos($str, 'foo')) {
    echo 'found ';
} else {
    echo 'notfound ';
}

// proper...
if (strpos($str, 'foo') !== false) {
    echo 'found ';
} else {
    echo 'notfound ';
}
避讳 2024-10-02 19:34:28

此正则表达式实现 rfc2822

[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\.[a-z0-9!#$%& '*+/=?^_{|}~-]+)*@(?:a-z0-9?.)+a-z0-9?

然而,在实践中,我发现这更适合通过网页捕获电子邮件地址(或更具体地说是 ADDR-SPEC):

^[a-z0-9._%+!$&*=^|~#%\'`?{}/-]+@[a-z0-9.-]+.[az] {2,6}$

This regex implements rfc2822:

[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*@(?:a-z0-9?.)+a-z0-9?

However, in practice, I find this more apposite for the purpose of capturing an email address (or more specifically an ADDR-SPEC) via a web page:

^[a-z0-9._%+!$&*=^|~#%\'`?{}/-]+@[a-z0-9.-]+.[a-z]{2,6}$

孤云独去闲 2024-10-02 19:34:28

欢迎您使用我的免费 PHP 函数 is_email() 来验证地址。可以在此处下载。它不使用正则表达式,因为我找不到完全实现 RFC 5321 的正则表达式。

is_email() 将确保地址完全符合 RFC 5321。它还可以选择检查域是否实际存在并具有 MX 记录。

您不应该依赖验证器来告诉您用户的电子邮件地址是否确实存在:一些 ISP 向其用户提供不合规的地址,特别是在不使用拉丁字母的国家/地区。更多关于电子邮件验证的文章请参见:http://isemail.info/about

You are welcome to use my free PHP function is_email() to validate addresses. It's available to download here. It doesn't use a regex because I can't find one that fully implements RFC 5321.

is_email() will ensure that an address is fully RFC 5321 compliant. It can optionally also check whether the domain actually exists and has an MX record.

You shouldn't rely on a validator to tell you whether a user's email address actually exists: some ISPs give out non-compliant addresses to their users, particularly in countries which don't use the Latin alphabet. More in my essay about email validation here: http://isemail.info/about.

岁月如刀 2024-10-02 19:34:28

这是使用 PHP 进行电子邮件验证检查的方法:

function check_email($email) {

  // Remove trailing and leading spaces.
  $email = trim($email);

  // Must have an @ sign.
  $at = strpos($email, '@');
  if( $at === false )
    return 1; //false;
  list($mailbox, $hostname) = explode('@', $email);

  // Check that there is a mailbox and a hostname
  if( $mailbox == '' || $hostname == '' )
    return 2; //false;

  // Only one @ allowed
  if( strpos($hostname, '@') !== false )
    return 3; //false;

  // Must be a . in the hostname
  if( strpos($hostname, '.') === false )
    return 4; //false;

  // Can't have a double in either mailbox or hostname
  if( strpos($hostname, '..') !== false || strpos($mailbox, '..') !== false )
    return 5; //false;

  // Mailbox can't start or end with a .
  if( substr($mailbox, 0, 1) == '.' || substr($mailbox, strlen($mailbox)-1, 1) == '.' )
    return 6; //false;

  // Hostname can't start or end with a .
  if( substr($hostname, 0, 1) == '.' || substr($hostname, strlen($hostname)-1, 1) == '.' )
    return 7; //false;

  // Check that all characters are valid
  if( str_replace(' ' , '', strtr(strtolower($mailbox), 'abcdefghijklmnopqrstuvwxyz0123456789!#$%&\'*+-/=?^_`{|}~.', '                                                        ')) != '' )
    return 8; // false;
  if( str_replace(' ' , '', strtr(strtolower($hostname), 'abcdefghijklmnopqrstuvwxyz0123456789_-.', '                                       ')) != '' )
    return 9; //false;

return 0; //true;
}

This is how you do an email validation check with PHP:

function check_email($email) {

  // Remove trailing and leading spaces.
  $email = trim($email);

  // Must have an @ sign.
  $at = strpos($email, '@');
  if( $at === false )
    return 1; //false;
  list($mailbox, $hostname) = explode('@', $email);

  // Check that there is a mailbox and a hostname
  if( $mailbox == '' || $hostname == '' )
    return 2; //false;

  // Only one @ allowed
  if( strpos($hostname, '@') !== false )
    return 3; //false;

  // Must be a . in the hostname
  if( strpos($hostname, '.') === false )
    return 4; //false;

  // Can't have a double in either mailbox or hostname
  if( strpos($hostname, '..') !== false || strpos($mailbox, '..') !== false )
    return 5; //false;

  // Mailbox can't start or end with a .
  if( substr($mailbox, 0, 1) == '.' || substr($mailbox, strlen($mailbox)-1, 1) == '.' )
    return 6; //false;

  // Hostname can't start or end with a .
  if( substr($hostname, 0, 1) == '.' || substr($hostname, strlen($hostname)-1, 1) == '.' )
    return 7; //false;

  // Check that all characters are valid
  if( str_replace(' ' , '', strtr(strtolower($mailbox), 'abcdefghijklmnopqrstuvwxyz0123456789!#$%&\'*+-/=?^_`{|}~.', '                                                        ')) != '' )
    return 8; // false;
  if( str_replace(' ' , '', strtr(strtolower($hostname), 'abcdefghijklmnopqrstuvwxyz0123456789_-.', '                                       ')) != '' )
    return 9; //false;

return 0; //true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文