发信人: hustoff (HUST Office), 信区: WebDev
标 题: 一个简单的PHP Validator,100行以内哦!
发信站: 武汉白云黄鹤站 (2005年05月14日16:57:30 星期六)
源文件下载:
http://byhh.net/cgi-bin/s/validator.php?n=validator.php&F=1116060990
class Validator
{
public $errMsgs = array();
public function Validate($queue)
{
$len = count($queue);
for ($i = 0; $i < $len; $i ++)
{
$objVal = trim($queue[$i][0]);
$func = $queue[$i][1];
/*$validateCondition = $queue[$i][2];[optional]*/
$allowNull = $queue[$i][3];
$errMsg = $queue[$i][4];
{
if ($this->$func($queue[$i]) === false)
{
$this->errMsgs[$i] = $errMsg;
}
}
}
}
public function FitRegEx($obj)
{
$re = $obj[2];
return !preg_match($re, $obj[0]) ? false : true;
}
public function AntiRegEx($obj)
{
$re = $obj[2];
return preg_match($re, $obj[0]) ? false : true;
}
public function NotBlank($obj)
{
return empty($obj[0]) ? false : true;
}
public function IsNumeric($obj)
{
return !is_numeric($obj[0]) ? false : true;
}
public function IsInt($obj)
{
$re = '/^[1-9][0-9]+$/';
return !preg_match($re, $obj[0]) ? false : true;
}
public function IsEmail($obj)
{
$re = '/^[a-zA-Z0-9_\.]+@([a-zA-Z0-9_-]+\.)+$/';
$tempArr = explode(".",$obj[0]);
$ext = $tempArr[count($tempArr)-1];
$tempStr = str_replace($ext,'',$obj[0]);
return (!preg_match($re,$tempStr) || !preg_match("/^[a-zA-Z]{2,4}$/",$ext) ? false : true;
}
public function IsPlainText($obj)
{
$re = '/<[a-zA-Z]+[^>]*>/';
return preg_match($re, $obj[0]) ? false : true;
}
public function LengthRange($obj)
{
$vcMin = intval(substr($obj[2],0,strpos($obj[2],"to")));
$vcMax = intval(substr($obj[2],strpos($obj[2],"to")+2));
return (strlen($obj[0]) < $vcMin || strlen($obj[0]) > $vcMax) ? false : true;
}
public function NumericRange($obj)
{
$vcMin = intval(substr($obj[2],0,strpos($obj[2],"to")));
$vcMax = intval(substr($obj[2],strpos($obj[2],"to")+2));
return (!is_numeric($obj[0]) || $obj[0] < $vcMin || $obj[0] > $vcMax) ? false : true;
}
public function IsEqualTo($obj)
{
return $obj[0] != $obj[2] ? false : true;
}
public function CheckLimit($obj)
{
$vcMin = intval(substr($obj[2],0,strpos($obj[2],"to")));
$vcMax = intval(substr($obj[2],strpos($obj[2],"to")+2)) == -1 ? count($obj[0]) : intval(substr($obj[2],strpos($obj[2],"to")+2));
return (count($obj[0]) < $vcMin || count($obj[0]) > $vcMax) ? false : true;
}
}
?>
你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=2447249