PHP电子邮件代码正则表达式


php email code regex

需要一些帮助与我的电子邮件验证。"very.unusual.@.unusual.com@example.com"应该是有效的电子邮件。但是php显示它是无效的。任何帮助都是很棒的

您可以在这里执行代码:http://sandbox.onlinephpfunctions.com/

    $unusual = "very.unusual.@.unusual.com@example.com";
$regex = "^[a-z0-9,!#'$%&''*'+/='?'^_`'{'|}~-]+('.[a-z0-9,!#'$%&''*'+/='?'^_`'{'|}~-]+)*@[a-z0-9-]+('.[a-z0-9-]+)*'.([a-z]{2,})$"; 
$normal = "^[a-z0-9_'+-]+('.[a-z0-9_'+-]+)*@[a-z0-9-]+('.[a-z0-9-]+)*'.([a-z]{2,4})$";
$domain = "[a-z0-9-]+('.[a-z0-9-]+)*'.([a-z]{2,})$"; 
$dom = strrchr($unusual,"@");
//find length of the needle
$len = strlen($dom);
//find postion
$position = strpos($unusual,$dom);
//cut the string
$begin = substr("$unusual",0,$position);

$end = substr("$unusual",$position+1);
//display it
echo"$begin'n"; 
echo "$end";
if ( eregi( $normal, $begin ) ) {
     echo $begin . " is a valid local. We can accept it.";
} else { 
     echo $begin . " is an invalid local. Please try again.";
} 
if ( eregi( $domain, $end ) ) {
     echo $end . " is a valid domain. We can accept it.";
} else { 
     echo $end . " is an invalid domain. Please try again.";
}

当PHP提供其他更容易阅读和理解的函数时,我不太喜欢使用regex。试试这个:

$email = "very.unusual.@.unusual.com@example.com";
if ( ! filter_var($email, FILTER_VALIDATE_EMAIL) === false) 
{
    echo "$email is a valid email address";
} 
else 
{
    echo "$email is not a valid email address";
}