Php 电子邮件验证中不推荐使用函数eregi()
大家好,我知道我们不是eregi,而是preg_match,但是当我只更改eregi代码时,它就不起作用了,如何更改下面的代码?请稍帮帮忙,我是个新手Php 电子邮件验证中不推荐使用函数eregi(),php,preg-match,eregi,Php,Preg Match,Eregi,大家好,我知道我们不是eregi,而是preg_match,但是当我只更改eregi代码时,它就不起作用了,如何更改下面的代码?请稍帮帮忙,我是个新手 function verify_valid_email($emailtocheck) { $eregicheck = "^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,4}\$"; retur
function verify_valid_email($emailtocheck)
{
$eregicheck = "^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,4}\$";
return eregi($eregicheck, $emailtocheck);
}
function verify_email_unique($emailtocheck)
{
global $config,$conn;
$query = "select count(*) as total from members where email='".mysql_real_escape_string($emailtocheck)."' limit 1";
$executequery = $conn->execute($query);
$totalemails = $executequery->fields[total];
if ($totalemails >= 1)
{
return false;
}
else
{
return true;
}
}
如果您需要验证电子邮件地址,可以查看页面,该页面提供了一个仅使用
filter\u var()
的工作示例:
因此,在您的代码中,您应该删除所有regex/eregi内容并使用以下内容:
return filter_var($emailtocheck, FILTER_VALIDATE_EMAIL);
如果您想这样做,您可以基于以下方法:
<?php
$email = \"abc123@somewhere\"; // Invalid email address
//$email = \"somebody@somesite.com\"; // Valid email address
// Set up regular expression strings to evaluate the value of email variable against
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
// Run the preg_match() function on regex against the email address
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.\";
}
?>
或:
资料来源:
或:
资料来源:
另外,你也可以试试安德烈·丹尼尔写的答案。你有很多选择。哪一部分“弃用”对你说“不管怎样,还是用我吧”?你只是想验证电子邮件的格式?我只是想了解如何更改此代码+1如果你可以使用
filter\u var
对一些自创的表达式进行过滤,认为它们有所有可能的电子邮件,我建议你这样做。虽然FILTER\u VALIDATE\u EMAIL
并不完美(据我所知),但它比自制版本要好。@Class我也同意你的看法。我想既然安德烈已经提到了这一点,我就不必在回答中重复了。OP有很多选择可供他选择。谢谢:)真是太棒了helpful@YunusEmreSarıgül非常欢迎你。
<?php
$email = \"abc123@somewhere\"; // Invalid email address
//$email = \"somebody@somesite.com\"; // Valid email address
// Set up regular expression strings to evaluate the value of email variable against
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
// Run the preg_match() function on regex against the email address
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.\";
}
?>
$string = "$emailtocheck";
if (preg_match(
'/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/',
$string)) {
echo "Successful.";
}
<?php
$email = "abc123@sdsd.com";
$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.";
}
?>
<?php
// check e-mail address
// display success or failure message
if (!preg_match("/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-
])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/", $_POST['e-mail'])) {
die("Invalid e-mail address");
}
echo "Valid e-mail address, processing...";
?>