插件35:文本验证

<?php // Plug-in 35: Validate Text

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

$text     = "This is some example text - let's test it!";
$allowed  = "a-zA-Z0-9 ";
$required = "";
$result = PIPHP_ValidateText($text, 1, 30, $allowed, $required);
echo "$text<br /><br />";

if ($result[0] == FALSE)
   for ($j = 0 ; $j < count($result[1]) ; ++$j)
      echo "  " . $result[1][$j] . ".<br>";
else echo "  Passed evaluation";

$text     = "password";
$allowed  = "a-zA-Z0-9 !&*+=:;@~#";
$required = "ludp";
$result = PIPHP_ValidateText($text, 10, 16, $allowed, $required);
echo "<br />$text<br /><br />";

if ($result[0] == FALSE)
   for ($j = 0 ; $j < count($result[1]) ; ++$j)
      echo "  " . $result[1][$j] . ".<br>";
else echo "  Passed evaluation";

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 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. The
   // arguments required 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
   
   $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);
}

?>

插件说明:

插件接受一个需要验证的字符串以及有关合法字符和非法字符等详细信息。验证失败则返回一个两元素数组。

其中第一个元素值是FALSE,第二个元素是一个代表错误信息的数组(如果验证通过,则返回一个元素的数组,它的值为TRUE)。具体如下:

$text 需要验证的文本

$minlength 允许的最小长度

$maxlength 允许的最大长度

$allowed 文本中允许出现的字符。他可以是任何字符,如用“a-"表示字符范围,如”a-zA-Z"

$required 必须输入的字符,即他们必须在文本中至少出现一次。他可能取a、1、字母、数字、单词(由字母或数字组成)或标点符号。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值