2013-08-27 98 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你非常歡迎。 –