函数eregi()在电子邮件验证中已弃用

时间:2013-08-27 01:30:50

标签: 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}\$";
    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;
    }
}

2 个答案:

答案 0 :(得分:4)

如果您需要验证电子邮件地址,可以查看this页面,该页面仅提供使用filter_var()的工作示例:

if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_a) email address is considered valid.";
};

所以在你的代码中,你应该删除所有正则表达式/ eregi的内容并改为使用它:

return filter_var($emailtocheck, FILTER_VALIDATE_EMAIL);

答案 1 :(得分:1)

如果你想这样做,你可以基于以下方法:

<?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.";
}           
?>

来源:https://stackoverflow.com/a/13719991/1415724

或:

<?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...";
?>

来源:http://www.techrepublic.com/article/regular-expression-engine-simplifies-e-mail-validation-in-php/


另外,你也可以试试AndréDaniel所写的答案。你有很多选择。