2012-09-21 55 views
2

我有問題,使用preg_match()驗證電子郵件地址。有效的電子郵件與preg_match

這是我的代碼塊。

if (VerifierAdresseMail($email)) 
    echo '<p>valide.</p>'; 
else 
    echo '<p>not valid</p>'; 


    function VerifierAdresseMail($adresse) { 
    $Syntaxe = '#^[\w.-][email protected][\w.-]+\.[a-zA-Z]{2,6}$#'; 
    if (preg_match($Syntaxe, $adresse)) 
     return true; 
    else 
     return false; 
    } 

這似乎不起作用。它只是返回一個白頁。我無法看到錯誤althought我有這個在我的application.ini

[developpement : production] 


phpSettings.display_startup_errors = 1 
phpSettings.display_errors = 1 
phpSettings.track_errors = 1 
phpSettings.error_reporting = E_ALL 

resources.frontController.params.displayExceptions = 1 

phpSettings.date.timezone = "Europe/Paris" 

感謝您的幫助

+1

它在ideone http://ideone.com/Jq7FL工作;你的正則表達式需要改進 –

回答

19

不要使用preg_match()驗證的電子郵件地址。使用filter_var()代替:

if (filter_var($adresse, FILTER_VALIDATE_EMAIL)) { 
    // valid 
} else { 
    // not valid 
}