Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 验证电子邮件地址的功能_Php_Email Address - Fatal编程技术网

Php 验证电子邮件地址的功能

Php 验证电子邮件地址的功能,php,email-address,Php,Email Address,我正在使用此功能验证电子邮件地址, 但如果电子邮件地址如下所示,则不起作用: name@server.com. OR //name@server.com 有没有办法开发这个功能 function validEmail($email) { $isValid = true; $atIndex = strrpos($email, "@"); if (is_bool($atIndex) && !$atIndex) { $isValid = fal

我正在使用此功能验证电子邮件地址, 但如果电子邮件地址如下所示,则不起作用:

name@server.com. 
OR 
//name@server.com
有没有办法开发这个功能

function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

最简单的

或与Asar相同但较短的:

function  checkEmail($email) {
     $patern = '/^[a-zA-Z0-9.\-_]+@[a-zA-Z0-9\-.]+\.[a-zA-Z]{2,4}$/';
     return preg_match($patern , $email);
}
使用。下面是一个简单的使用演示。还有许多其他选择

<?php

  // You might want to trim whitespace first: 
$possibleEmailAddress = trim($possibleEmailAddress);

filter_var($possibleEmailAddress, FILTER_VALIDATE_EMAIL);

// Returns false if $possibleEmailAddress doesn't appear valid.
// Returns the email string if it does appear okay.
?>

注意//name@server.com是有效的电子邮件,但是name@server.com. 事实并非如此。你必须把句号从结尾处删减,使之有效。你不能只使用trim,因为电子邮件开头的句点可能是有效的和有意的。

。有许多正则表达式是为了验证电子邮件而创建的。只需google email regex,你就会得到很多结果。你说得对,thx Thu,我正在检查筛选函数thx你对$domain的preg_匹配限制太严格了-域名中允许使用非ASCII字符,例如èè.com是有效的域名。我还认为规范没有禁止在地址的本地部分使用初始点、最终点或连续点。可能存在重复和错误。甚至不考虑本地部分的+符号。拒绝现有顶级域名,如.museumRejects.museum域名等,拒绝我的一个实际电子邮件地址,其中有一个+。我只是简化了Asar的代码和正则表达式;感谢大家投票否决>
<?php

  // You might want to trim whitespace first: 
$possibleEmailAddress = trim($possibleEmailAddress);

filter_var($possibleEmailAddress, FILTER_VALIDATE_EMAIL);

// Returns false if $possibleEmailAddress doesn't appear valid.
// Returns the email string if it does appear okay.
?>