检查字符串是否是 PHP 中的电子邮件地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1725907/
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
Check if a string is an email address in PHP
提问by homework
I am trying to do an SQL query, but I need to check somehow if the value is an email address.
I need a way to check if $useris an email address, because I have user values such as this in my table.
我正在尝试执行 SQL 查询,但我需要以某种方式检查该值是否为电子邮件地址。我需要一种方法来检查是否$user是电子邮件地址,因为我的表中有这样的用户值。
test
test2
[email protected]
[email protected]
test392
[email protected]
and so on...
等等...
I need to make it so $useremailchecks $userto find if it's an email address. So I can UPDATE the values, WHERE user=test OR [email protected], etc.
我需要这样做,以便$useremail检查$user它是否是电子邮件地址。所以我可以更新值WHERE user=test OR [email protected],等等。
$user = strtolower($olduser);
$useremail = "";
mysql_query("UPDATE _$setprofile SET user=$sn, fc=$fc WHERE user='$user' OR user='$useremail");
采纳答案by Jake
This is not a great method and doesn't check if the email exists but it checks if it looks like an email with the @ and domain extension.
这不是一个很好的方法,它不会检查电子邮件是否存在,但会检查它是否看起来像带有 @ 和域扩展名的电子邮件。
function checkEmail($email) {
$find1 = strpos($email, '@');
$find2 = strpos($email, '.');
return ($find1 !== false && $find2 !== false && $find2 > $find1);
}
$email = '[email protected]';
if ( checkEmail($email) ) {
echo $email . ' looks like a valid email address.';
}
回答by Uri
Without regular expressions:
没有正则表达式:
<?php
if(filter_var("[email protected]", FILTER_VALIDATE_EMAIL)) {
// valid address
}
else {
// invalid address
}
?>
回答by Travis
The simplest approach is to use a regular expression to check email addresses, although there's some disagreement about how accurate this can be. This process is discussed in detail here:
最简单的方法是使用正则表达式来检查电子邮件地址,尽管对于它的准确性存在一些分歧。此处详细讨论此过程:
Using a regular expression to validate an email address
You can use REGEXP in MySQL to select from the database based on your regular expression:
您可以在 MySQL 中使用 REGEXP 根据您的正则表达式从数据库中进行选择:
回答by Ben
if(filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo 'This is a valid email address.';
echo filter_var($email, FILTER_VALIDATE_EMAIL);
//exit("E-mail is not valid");
}
else
{
echo 'Invalid email address.';
}
回答by Rubens Farias
You can use regular expressions to validate your input string to see if it matches an email address:
您可以使用正则表达式来验证您的输入字符串以查看它是否与电子邮件地址匹配:
<?php
$email = "[email protected]";
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
echo "Valid email address.";
}
else {
echo "Invalid email address.";
}
?>
From: http://www.totallyphp.co.uk/code/validate_an_email_address_using_regular_expressions.htm
来自:http: //www.totallyphp.co.uk/code/validate_an_email_address_using_regular_expressions.htm
EDIT: for more accurate expressions, please refer to Travis answeron this question
编辑:有关更准确的表达,请参阅Travis对此问题的回答
回答by Dominic Sayers
This function is_email()will give you an definite answer to whether the string is a valid email address or not. As far as I know, no other solution will do this with the same level of authority.
这个函数is_email()会给你一个关于字符串是否是有效电子邮件地址的明确答案。据我所知,没有其他解决方案可以以相同级别的权限做到这一点。
If I understand the example correctly, you would use it like this
如果我正确理解了这个例子,你会像这样使用它
$user = strtolower($olduser);
$useremail = (is_email($user)) ? $user : '';
The PHP built-in function is incomplete. I'm the principle author of is_email()so I'm blowing my own trumpet here, but I've put a lot of work into this in order that nobody else should have to ever again.
PHP 内置函数不完整。我是主要作者,is_email()所以我在这里吹自己的小号,但我已经投入了大量的工作,以便其他人不再需要。
回答by Rob
I've been using this function for many years across hundreds of sites. It's probably not perfect, but i've never had a complaint of false negatives (or positives):
多年来,我一直在数百个站点上使用此功能。它可能并不完美,但我从来没有抱怨过假阴性(或阳性):
function validate_email($email) {
return (preg_match("/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/", $email) || !preg_match("/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/", $email)) ? false : true;
}
回答by BalusC
Apart from all regex suggestions which mostly doesn't support allof the available TLD's (including for example .infoand .museum) and the upcoming ICANN decision to allow Chinese and Arabic in URL's (which would let [a-z]and \wfail) for which the following more generic regex is sufficient
除了大多数不支持所有可用 TLD(包括例如.info和.museum)的所有正则表达式建议以及即将到来的 ICANN 决定允许在 URL 中使用中文和阿拉伯语(这将允许[a-z]和\w失败)之外,以下更通用的正则表达式就足够了
([^.@]+)(\.[^.@]+)*@([^.@]+\.)+([^.@]+)
I would also mention that doing a WHERE user=test OR [email protected]is too error prone. Better use an autogenerated PK in the table and use it in the WHEREclause.
我还要提到做 aWHERE user=test OR [email protected]太容易出错了。最好在表中使用自动生成的 PK 并在WHERE子句中使用它。
I don't really see the value of strict, long and unreadable mailregexes conforming the RFCxxxx specs. Best way is still to send a mail with a link with an activation key to the end user to let it confirm the mail address. Inform that in the form as well. If necessary let the user type the email address twice like you do for passwords.
我没有真正看到符合 RFCxxxx 规范的严格、长且不可读的邮件正则表达式的价值。最好的方法仍然是向最终用户发送带有激活密钥链接的邮件,让其确认邮件地址。也以表格形式告知。如有必要,让用户像输入密码一样输入电子邮件地址两次。
回答by Scott
Since everybody is posting their regular expression, here's mine:
^((([\w+-]+)(.[\w+-]+)*)|(\"[^(\|\")]{0,62}\"))@(([a-zA-Z0-9-]+.)+([a-zA-Z0-9]{2,})|[?([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})(.([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})){3}]?)$
由于每个人都发布了他们的正则表达式,这是我的:
^((([\w+-]+)(.[\w+-]+)*)|(\"[^(\|\")]{0,62}\"))@(([a-zA-Z0-9-]+.)+([a-zA-Z0-9]{2,})|[?([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})(.([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})){3}]?)$
To the best of my knowledge, it supports everything in the RFC specification, including a lot of things that most people don't usually include.
据我所知,它支持 RFC 规范中的所有内容,包括许多大多数人通常不包含的内容。
回答by iamkoa
$email = "[email protected]";
function isEmail($email) {
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
return true
} else {
return false
}
}
if (isEmail($user_email)) {
// email is valid, run the query
mysql_query("UPDATE _$setprofile SET user=$sn, fc=$fc WHERE user='$user' OR user='$user_email");
}

