先用一个小例子介绍一下此函数怎么使用
整数的正则校验是`'/^[0-9]*$/'`
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form name="test" id="test" action="./test_1.php" method="post" placehold="name">
必须输入数字:<input type="text" name="number" id="age" value="" placeholder="请输入一个数字"><br/>
<input type="submit" value="确定">
form>
body>
html>
//验证类别
define("VALIDATE_EMPTY","1"); //验证为空
define("VALIDATE_INT_RANGE","2"); //验证整数
define("VALIDATE_REG","3"); //验证正则
define("VALIDATE_MASK","4"); //验证正则
define("VALIDATE_FUNCTION","5"); //验证函数
define("VALIDATE_SELECT","6"); //验证select
//正则表达式
define("PHP_REG_VALIDATE_INT",'/^[0-9]*$/');
class ValidateCommit
{
public static function validate($validate_url, $validate_reg)
{
//头上没有加验证参数配置,默认为TRUE
if(!is_array($validate_reg)) {
return true;
}
//验证结果
$validate_ret = true;
self::v($validate_reg,$validate_ret);
if($validate_ret === false) {
self::out($validate_url,$validate_ret);
}
}
private function out($validate_url,$validate_ret) {
echo "DEBUG-- File:".__FILE__.", Fun:".__FUNCTION__.", Line:".__LINE__." 校验结果:".var_export($validate_ret, true)."
";
exit;
}
/**
* 获取页面提交的数据,以及校验标准;对提交的数据进行拆分提交,按照标准进行校验
*
* @param [type] $validate_reg 校验的标准,
* 即 array(2) { ["age"]=> array(2) { ["type"]=> int(0)
* ["val"]=> array(4) { [0]=> string(3) "1|2" [1]=> string(16) "/^[0-9]\d{0,2}$/" [2]=> int(1) [3]=> int(255) } }
* @param [type] $validate_ret 返回值
* @return void
*/
private function v($validate_reg,&$validate_ret) {
echo "
"."DEBUG-- File:".__FILE__.", Fun:".__FUNCTION__.", Line:".__LINE__."
";
if($validate_ret === true && is_array($validate_reg)) {
foreach($validate_reg as $k=>$v) {
//此时的key是post内的值
echo "
******DEBUG-- File:".__FILE__.", Fun:".__FUNCTION__.", Line:".__LINE__." 开始校验:".var_export($k, true)."
";
if(array_key_exists("sel_val",$v)) {
//级联的验证
$tk = "sel_".$v["sel_val"];
if(is_array($v[$tk])) {
self::v($v[$tk],$validate_ret);
}
} else {
//单次验证
$validate_vaule = $_POST[$k]; //post后的值赋值给 validate_value
if(array_key_exists('type',$v)) {
if($v['type'] == 1) {
$validate_vaule = $_GET[$k]; //若type的值是1,则获取get值,赋值给 validate_value
} else if($v['type'] == 2) {
$validate_vaule = $_REQUEST[$k]; //若type的值是2,则获取request值,赋值给 validate_value
}
}
/**
* "radio_one"=>array(
* "type"=>0,
* "mutiple_val"=>array("0"=>array('1|2'),"1"=>array('1|2'))
*)
*/
if(is_array($validate_vaule)) {
//若提交过来的值是数组
var_dump("11111111111111111111提交过来的值是数组");
if(array_key_exists('mutiple_val',$v)) {
//不同方式检测
foreach($v['mutiple_val'] as $sub_k => $sub_v) {
$val = $validate_vaule[$sub_k];
$tarr = explode("|",$sub_v[0]);
$validate_flag = 0;
foreach($tarr as $tv) {
$validate_ret = self::vone($tv,$val,$sub_v);
if($validate_ret === false) {
$validate_flag = 1;
break;
}
}
if($validate_flag = 1) break;
}
} else if(array_key_exists('val',$v)) {
//全部一种方式检测
$tarr = explode("|",$v['val'][0]);
foreach($tarr as $tv) {
$validate_flag = 0;
foreach($validate_vaule as $key => $val) {
$validate_ret = self::vone($tv,$val,$v['val']);
if($validate_ret === false) {
$validate_flag = 1;
break;
}
}
if($validate_flag) {
break;
}
}
}
} else {
//若提交过来的值不是数组
//$valicheck['ipaddr_str']=array("type"=>0,"val"=>array('1|5','checkValidateAreaIp'));
var_dump("222222222222222222222222222222222222若提交过来的值不是数组");
$tarr = explode("|",$v['val'][0]);
foreach($tarr as $tv) {
$validate_ret = self::vone($tv,$validate_vaule,$v['val']);
if($validate_ret === false) {
break;
}else{
echo "DEBUG-- File:".__FILE__.", Fun:".__FUNCTION__.", Line:".__LINE__." 校验结果:".var_export($validate_ret, true)."
";
}
}
}
}
if($validate_ret === false) {
break;
}
}
}
return $validate_ret;
}
/**
* 判断参数的合法性
* array(1) { ["age"]=> array(2) { ["type"]=> int(0) ["val"]=> array(4) { [0]=> string(3) "1|2" [1]=> string(16) "/^[0-9]\d{0,2}$/" [2]=> int(1) [3]=> int(255) } } }
*
* 根据arr["val"][0]; 在arr["val"][1]、arr["val"][2]、arr["val"][3] 规则下,判断提交的值的合法性 即:string(1) "1"
* @param [type] $vtype arr["val"][0]的值、一次只传入1个,在传入前对其按照|进行了分割 即:string(2) "25"
* @param [type] $val post/get/request提交的值
* @param [type] $reg arr["val"] 即:array(4) { [0]=> string(3) "1|2" [1]=> string(16) "/^[0-9]\d{0,2}$/" [2]=> int(1) [3]=> int(255) }
* @return void
*/
private function vone($vtype,$val,$reg) {
echo "
"."DEBUG-- File:".__FILE__." Fun:".__FUNCTION__.", Line:".__LINE__."
校验类型:".$vtype." 被校验的值:".$val." 校验规则:".var_export($reg, true)."
";
switch ($vtype) {
case VALIDATE_EMPTY: //1:不为空,但可以为0
if(empty($val) && $val !=0) {
return false;
}
break;
case VALIDATE_INT_RANGE: //2:正则匹配$reg[1]; $reg[2]<=$val<=$reg[3]
if (!(preg_match($reg[1], $val) && $val >= $reg[2] && $val <= $reg[3])) {
return false;
}
break;
case VALIDATE_REG: //3:正则匹配$reg[1]
if (!preg_match($reg[1], $val)) {
return false;
}
break;
case VALIDATE_MASK: //4:正则匹配$reg[1]; 正则匹配$reg[2];
if (!(preg_match($reg[1], $val) || preg_match($reg[2], $val))) {
return false;
}
break;
case VALIDATE_FUNCTION: //5:函数校验
if($reg[1]=='checkValidatePwdChk'){
if (!($reg[1]($val,$reg[2],$reg[3]))) {
return false;
}
}else{
if (!($reg[1]($val))) {
//$reg[1]为函数名,$val为校验准则
return false;
}
}
break;
case VALIDATE_SELECT: //6:函数校验
if (!(checkValidateSelectIn($val,$reg[1]))) {
return false;
}
break;
default:
break;
}
return true;
}
}
echo "
"."DEBUG-- File:".__FILE__.", Line:".__LINE__." POST/GET/REQUEST的所有数据如下:
";
var_dump($_POST);echo "
";
/**type为0: post值
* type为1: get值
* type为2: request值
*/
//type:0-->post类型的number参数; val的array 3-->$_POST['number']的值符合ipv4格式; [3:正则,单次匹配]
$valicheck['number']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_INT));
ValidateCommit::validate("index.html",$valicheck);?>
//地址匹配:1.1.1.1(仅仅)
define("PHP_REG_VALIDATE_IP",'/^((?:(?:25[0-5]|2[0-4]\d|((1\d{
2})|([1-9]?\d)))\.){
3}(?:25[0-5]|2[0-4]\d|((1\d{
2})|([1-9]?\d))))$/');
define("PHP_REG_VALIDATE_IP_1",'/^(((\d{
1})|(\d{
2})|(1\d{
2})|(2[0-4]\d{
1})|(25[0-5]))\.){
3}((\d{
1})|(\d{
2})|(1\d{
2})|(2[0-4]\d{
1})|(25[0-5]))$/');
//地址匹配:192.168.1.1/24
define("PHP_REG_VALIDATE_IP_INTMASK",'/^(\d{
1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{
1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{
1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{
1,2}|1\d\d|2[0-4]\d|25[0-5])\/([0-9]|1\d|2\d|3[0-2])$/');
//地址匹配:192.168.1.1/255.255.255.0
define("PHP_REG_VALIDATE_IP_IPMASK",'/^(\d{
1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{
1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{
1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{
1,2}|1\d\d|2[0-4]\d|25[0-5])\/((254|252|248|240|224|192|128|0)\.0\.0\.0|255\.(254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(255|254|252|248|240|224|192