-4

嗨,我知道我们不是 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;
    }
}
4

2 回答 2

4

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

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

因此,在您的代码中,您应该只删除所有 regex/eregi 的东西并改用它:

return filter_var($emailtocheck, FILTER_VALIDATE_EMAIL);
于 2013-08-27T01:43:04.733 回答
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 写的作为答案。你有很多选择。

于 2013-08-27T01:49:07.403 回答