插件36:验证Email地址

<?php // Plug-in 36: Validate Email

// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$email  = "paul.smith@smithandson.com";
$result = PIPHP_ValidateEmail($email);
echo "The email address: '$email' ";
echo $result ? "validates" : "does not validate";

$email  = "jdoe@usacom";
$result = PIPHP_ValidateEmail($email);
echo "<br />The email address: '$email' ";
echo $result ? "validates" : "does not validate";

function PIPHP_ValidateEmail($email)
{
   // Plug-in 36: Validate Email
   //
   // This plug-in takes an email address and determines whether
   // it appears to be valid. The argument required is:
   //
   //    $email: An email address to validate
   
   $at = strrpos($email, '@');
   
   if (!$at || strlen($email) < 6) return FALSE;
   
   $left  = substr($email, 0, $at);
   $right = substr($email, $at + 1);
   $res1  = PIPHP_ValidateText($left,  1, 64,  "\w\.\+\-",
      "a");
   $res2  = PIPHP_ValidateText($right, 1, 255, "\a-zA-Z0-9\.\-",
      "a");
  
   if (!strpos($right, '.') || !$res1[0] || !$res2[0])
      return FALSE;
   else return TRUE;
}

// The function below is there to ensure that the above function
// (which relies on it) has access it it.

function PIPHP_ValidateText($text, $minlength, $maxlength,
   $allowed, $required)
{
   // Plug-in 35: Validate Text
   // This plug-in takes a string and parameters defining its
   // minimum and maximum length, and the allowed characters.
   // The arguments are:
   //    $text:      The text to be validate
   //    $minlength: The minimum allowed length
   //    $maxlength: The maximum allowed length
   //    $allowed:   The allowed characters. Can include regexp
   //                strings such as a-zA-Z0-9 or \w. Characters
   //                used in regular expressions but which are
   //                to be allowed (such as ( and [ etc) should
   //                be escaped, like this: \( and \[.
   //    $required:  The required characters. This argument
   //                is a string containing one or more of the
   //                letters a, l, u, d, w or p for any letter,
   //                lower case, upper case, digit, word (any
   //                lower, upper, digit or _) or punctuation.
   //                For each of these included, at least one of
   //                that type of character must be in the string
   // The plug-in returns an array of two elements if the string
   // does not validate. The first has the value FALSE and the
   // second is an array of error messages. If it does validate
   // Only one element is returned and its value is TRUE.
   
   $len   = strlen($text);
   $error = array();
   
   if ($len < $minlength)
      $error[] = "The string length is too short " . 
         "(min $minlength characters)";
   elseif ($len > $maxlength)
      $error[] = "The string length is too long " .
         "(max $maxlength characters)";
   
   $result = preg_match_all("/([^$allowed])/", $text, $matches);
   $caught = implode(array_unique($matches[1]), ', ');
   $plural = strlen($caught) > 1 ? $plural = "s are" : " is";

   if ($result) $error[] = "The following character$plural " .
      "not allowed: " . $caught;

   for ($j = 0 ; $j < strlen($required) ; ++$j)
   {
      switch(substr(strtolower($required), $j, 1))
      {
         case "a": $regex = "a-zA-Z"; $str = "letter";
                   break;
         case "l": $regex = "a-z";    $str = "lower case";
                   break;
         case "u": $regex = "A-Z";    $str = "upper case";
                   break;
         case "d": $regex = "0-9";    $str = "digit";
                   break;
         case "w": $regex = "\w";     $str = "letter, number or _";
                   break;
         case "p": $regex = "\W";     $str = "punctuation";
                   break;
      }

      if (!preg_match("/[$regex]/", $text))
         $error[] = "The string must include at least one " .
            "$str character";
   }

   if (count($error)) return array(FALSE, $error);
   else return array(TRUE);
}

?>

插件说明:

插件接受一个需要验证格式的email地址,验证成功返回TRUE,否则返回FALSE。

需要参数:

$email:需要验证的email地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值