首页 > 开发 > PHP > 正文

牛人写的验证EMAIL函数

2024-05-04 22:58:37
字体:
来源:转载
供稿:网友

转自haohappy's blog里的一个email验证函数

<?php
function valid_email($email) {
  // first, we check that there's one @ symbol, and that the lengths are right
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    // email invalid because wrong number of characters in one section, or wrong number of @ symbols.
    return false;
  }
  // split it into sections to make life easier
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
     if (!ereg("^(([a-za-z0-9!#$%&'*+/=?^_`{|}~-][a-za-z0-9!#$%&'*+/=?^_`{|}~/.-]{0,63})|(/"[^(//|/")]{0,62}/"))$", $local_array[$i])) {
      return false;
    }
  }  
  if (!ereg("^/[?[0-9/.]+/]?$", $email_array[1])) { // check if domain is ip. if not, it should be valid domain name
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false; // not enough parts to domain
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if (!ereg("^(([a-za-z0-9][a-za-z0-9-]{0,61}[a-za-z0-9])|([a-za-z0-9]+))$", $domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}
?>
 


 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表