2013-08-27 52 views
-4

您好我知道我们不eregi但preg_match,但是当我只更改eregi代码它不工作,如何更改下面的代码只需要一点帮助,我是一个新手函数eregi()在电子邮件验证中已被弃用

function verify_valid_email($emailtocheck) 
{ 
    $eregicheck = "^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])[email protected]([-!#\$%&'*+/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; 
    } 
} 
+1

“弃用”的哪一部分对您说“请继续使用我”? –

+0

您只想验证电子邮件的格式? –

+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我推荐这个,如果你可以使用'filter_var'而不是家庭酿造的表达式,以为他们获得了所有可能的邮件。尽管'FILTER_VALIDATE_EMAIL'并不完美(据我所知),但它比家庭酿造的版本更好。 – Class

+0

@ Class我同意你的看法。自从安德烈已经提到它之后,我想,我没有在我的答案中重复它。 OP有许多可供选择的选项。 –

1

如果你想这样做,这样,你就可以立足自己在下面的方法:

<?php 
$email = \"[email protected]\"; // Invalid email address 
//$email = \"[email protected]\"; // 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 = "abc12[email protected]"; 
$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/


另外,你可以尝试什么安德烈·丹尼尔作为一个答案写为好。你有很多选择。

+1

谢谢:)这是非常有帮助 –

+0

@YunusEmreSarıgül你非常欢迎。 –