文章目录 1、 使用一个例子介绍1》 html页面显示test_1.html页面代码 2》html页面提交的数据3》php校验函数test_1.php校验代码 4》校验结果 2
先用一个小例子介绍一下此函数怎么使用
整数的正则校验是`'/^[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; } 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 } } 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; } 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类型的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/24define("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|128|0))$/');//ipv6地址匹配: ::~ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 不支持2006:19/64 不支持2006:9-2006::19define("PHP_REG_VALIDATE_IPV6",'/^([\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^::([\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:):([\da-fA-F]{1,4}:){0,3}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){2}:([\da-fA-F]{1,4}:){0,2}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){3}:([\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){4}:((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$|^:((:[\da-fA-F]{1,4}){1,6}|:)$|^[\da-fA-F]{1,4}:((:[\da-fA-F]{1,4}){1,5}|:)$|^([\da-fA-F]{1,4}:){2}((:[\da-fA-F]{1,4}){1,4}|:)$|^([\da-fA-F]{1,4}:){3}((:[\da-fA-F]{1,4}){1,3}|:)$|^([\da-fA-F]{1,4}:){4}((:[\da-fA-F]{1,4}){1,2}|:)$|^([\da-fA-F]{1,4}:){5}:([\da-fA-F]{1,4})?$|^([\da-fA-F]{1,4}:){6}:$/');//掩码匹配:255.255.255.0 类型的掩码define("PHP_REG_VALIDATE_MASK",'/^(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\.(254|252|248|240|224|192|128|0))$/');//掩码匹配:0~32 掩码define("PHP_REG_VALIDATE_MASKINT",'/^([0-9]|1[0-9]|2[0-9]|3[0-2])$/');//端口匹配:0define("PHP_REG_VALIDATE_PORT",'/^([1-9]\d{0,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/');//端口匹配:0<=port<=65535define("PHP_REG_VALIDATE_PORT_ZERO",'/^(\d{0,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/');//日期匹配: 2016-2-16前面的0可以不写define("PHP_REG_VALIDATE_DATE",'/^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])$/');//日期匹配: 2016-02-16前面的0必须写define("PHP_REG_VALIDATE_DATE_ZERO",'/^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/');//时间匹配: 00:00|23:59|10:10 不匹配:24:00|00:60|25:61define("PHP_REG_VALIDATE_TIME",'/^(([1-9]{1})|([0-1][0-9])|([1-2][0-3])):([0-5][0-9])$/');//日期+时间匹配: 2016-02-16 23:59:59 前面的0必须写define("PHP_REG_VALIDATE_DATETIME_ZERO",'/^\d{4}[\-](0[1-9]|1[012])[\-](0[1-9]|[12][0-9]|3[01])(\s+(0[0-9]|1[0-9]|2[0-3])\:(0[0-9]|[1-5][0-9])\:(0[0-9]|[1-5][0-9]))$/');define("PHP_REG_VALIDATE_DATETIME_ZERO_1",'/^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/');//日期+时间匹配: 2016-2-16 或者 2016-02-16 23:59:59 前面的0可以不写define("PHP_REG_VALIDATE_DATETIME",'/^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])(\s+(0?[0-9]|1[0-9]|2[0-3])\:(0?[0-9]|[1-5][0-9])\:(0?[0-9]|[1-5][0-9]))?$/');//Mac地址匹配: 匹配ff:ff:ff:ff:ff:ffdefine("PHP_REG_VALIDATE_MAC",'/^([0-9a-fA-F]{2})(([\s:][0-9a-fA-F]{2}){5})$/');
DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Titletitle> <style type="text/CSS"> input {width: 1000px;} style>head><body><form name="test" id="test" action="./test.php" method="post" placehold="name"> 年龄:<input type="number" name="age" id="age" value="" placeholder="例如:1~255"><br/> IPV4:<input type="text" name="ipv4" id="ipv4" value="" placeholder="例如:1.1.11.1"><br/> IPV4_1:<input type="text" name="ipv4_1" id="ipv4_1" value="" placeholder="例如:192.168.1.1/24"><br/> IPV4_2:<input type="text" name="ipv4_2" id="ipv4_2" value="" placeholder="例如:192.168.1.1/255.255.255.0"><br/> 掩码:<input type="text" name="mask" id="mask" value="" placeholder="例如:255.255.255.0"><br/> 掩码_1:<input type="text" name="mask_1" id="mask_1" value="" placeholder="例如:0~32"><br/> IPV6:<input type="text" name="ipv6" id="ipv6" value="" placeholder="例如: ::~ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"><br/> IPV4/6:<input type="text" name="ipv4-6" id="ipv4-6" value="" placeholder="例如: 1.1.11.1 或 ::~ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"><br/> mac:<input type="text" name="mac" id="mac" value="" placeholder="例如: ff:ff:ff:ff:ff:ff"><br/> 端口:<input type="number" name="port" id="port" value="" placeholder="例如:0" ><br/> 端口_1:<input type="number" name="port_1" id="port_1" value="" placeholder="例如:0<=port<=65535"><br/> 日期:<input type="text" name="date" id="date" value="" placeholder="例如:2016-2-16前面的0可以不写 不匹配:2016-02-16 23:59:59"><br/> 日期_1:<input type="text" name="date_1" id="date_1" value="" placeholder="例如:2016-02-16前面的0必须写"><br/> 时间:<input type="text" name="time" id="time" value="" placeholder="例如:00:00|23:59|10:10 不匹配:24:00|00:60|25:61"><br/> 日期+时间:<input type="text" name="datetime" id="datetime" value="" placeholder="例如:2016-02-16 23:59:59 前面的0必须写"><br/> 日期+时间_1:<input type="text" name="datetime_1" id="datetime_1" value="" placeholder="例如:00:00|23:59|10:10 2016-2-16 或者 2016-02-16 23:59:59 前面的0可以不写"><br/> 密码:<input type="passWord" name="password" id="password" value="" placeholder="数字、字母、特殊字符(~!@#$%^&*-+,.;|><)中的两者,且大于等于8位小于等于10位"><br/> 名称:<input type="text" name="name" id="name" value="" placeholder="字符串可为空;不为空时,由字母、数字、下划线、中划线、点和中文组成,1-31个字符,且不能以NMC_ 开头"><br/> 字符串在数组内:<input type="text" name="select" id="select" value="" placeholder="输入的字符串为 'aaa','bbb','ccc'"><br /> ipv4Area:<textarea rows="5" cols="50" name="ipv4area" placeholder="空或All或者1.1.1.1\r1.1.1.1-5.5.5.5\r192.168.1.10/24\r192.168.1.20/255.255.255.0">textarea><br/> ipv4Area_1:<textarea rows="5" cols="50" name="ipv4area_1" placeholder="空或All或者1.1.1.1\r1.1.1.1-5.5.5.5 不匹配192.168.1.10/24\r192.168.1.20/255.255.255.0">textarea><br/> ipv4Area_2:<textarea rows="5" cols="50" name="ipv4area_2" placeholder="空或All或者1.1.1.1/24\r1.1.1.1/255.255.255.0 不匹配1.1.1.1\r1.1.1.1-5.5.5.5">textarea><br/> ipv6Area:<textarea rows="5" cols="50" name="ipv6area" placeholder="空或All或者2006::19\r\n2006::9-2006::19\r\n2006:19/64">textarea><br/> mac地址:<textarea rows="6" cols="50" name="macarea" placeholder="空或00:23:24:5D:58:71 不匹配ff:ff:ff:ff:ff:ff">textarea><br/> 端口area:<textarea rows="6" cols="50" name="portarea" placeholder="空或0" >textarea><br/> tcp/udp端口:<textarea rows="6" cols="50" name="port_area" name="port_area" id="port" value="" placeholder="一行一组端口, 格式说明:
80 表示目的端口为 80 的服务
81-90 表示目的端口范围为 81-90 的服务
222/333 表示源端口为 222, 目的端口为 333 的服务
100-200/80-90 表示源端口范围为 100-200, 目的端口范围为 80-90 的服务 ">textarea><br/> ip端口:<textarea rows="6" cols="50" name="ipPort" id="ipPort" value="" placeholder="0~255">textarea><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//正则表达式//地址匹配: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/24define("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|128|0))$/');//ipv6地址匹配: ::~ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 不支持2006:19/64 不支持2006:9-2006::19define("PHP_REG_VALIDATE_IPV6",'/^([\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^::([\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:):([\da-fA-F]{1,4}:){0,3}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){2}:([\da-fA-F]{1,4}:){0,2}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){3}:([\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){4}:((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$|^:((:[\da-fA-F]{1,4}){1,6}|:)$|^[\da-fA-F]{1,4}:((:[\da-fA-F]{1,4}){1,5}|:)$|^([\da-fA-F]{1,4}:){2}((:[\da-fA-F]{1,4}){1,4}|:)$|^([\da-fA-F]{1,4}:){3}((:[\da-fA-F]{1,4}){1,3}|:)$|^([\da-fA-F]{1,4}:){4}((:[\da-fA-F]{1,4}){1,2}|:)$|^([\da-fA-F]{1,4}:){5}:([\da-fA-F]{1,4})?$|^([\da-fA-F]{1,4}:){6}:$/');//掩码匹配:255.255.255.0 类型的掩码define("PHP_REG_VALIDATE_MASK",'/^(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\.(254|252|248|240|224|192|128|0))$/');//掩码匹配:0~32 掩码define("PHP_REG_VALIDATE_MASKINT",'/^([0-9]|1[0-9]|2[0-9]|3[0-2])$/');//端口匹配:0define("PHP_REG_VALIDATE_PORT",'/^([1-9]\d{0,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/');//端口匹配:0<=port<=65535define("PHP_REG_VALIDATE_PORT_ZERO",'/^(\d{0,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/');//日期匹配: 2016-2-16前面的0可以不写define("PHP_REG_VALIDATE_DATE",'/^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])$/');//日期匹配: 2016-02-16前面的0必须写define("PHP_REG_VALIDATE_DATE_ZERO",'/^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/');//时间匹配: 00:00|23:59|10:10 不匹配:24:00|00:60|25:61define("PHP_REG_VALIDATE_TIME",'/^(([1-9]{1})|([0-1][0-9])|([1-2][0-3])):([0-5][0-9])$/');//日期+时间匹配: 2016-02-16 23:59:59 前面的0必须写define("PHP_REG_VALIDATE_DATETIME_ZERO",'/^\d{4}[\-](0[1-9]|1[012])[\-](0[1-9]|[12][0-9]|3[01])(\s+(0[0-9]|1[0-9]|2[0-3])\:(0[0-9]|[1-5][0-9])\:(0[0-9]|[1-5][0-9]))$/');define("PHP_REG_VALIDATE_DATETIME_ZERO_1",'/^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/');//日期+时间匹配: 2016-2-16 或者 2016-02-16 23:59:59 前面的0可以不写define("PHP_REG_VALIDATE_DATETIME",'/^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])(\s+(0?[0-9]|1[0-9]|2[0-3])\:(0?[0-9]|[1-5][0-9])\:(0?[0-9]|[1-5][0-9]))?$/');//mac地址匹配: 匹配ff:ff:ff:ff:ff:ffdefine("PHP_REG_VALIDATE_MAC",'/^([0-9a-fA-F]{2})(([\s:][0-9a-fA-F]{2}){5})$/');define("PHP_REG_VALIDATE_EMAIL",'/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/');define("PHP_REG_VALIDATE_PWD",'/^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)[a-zA-Z\d~!@#$%^*-+,.;|><]+.{8,}$/');define("PHP_REG_VALIDATE_INT",'/^[0-9]*$/');define("PHP_REG_VALIDATE_PHONE",'/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/');define("PHP_REG_VALIDATE_longitude",'/^(\-|\+)?(((\d|[1-9]\d|1[0-7]\d|0{1,3})\.\d{0,6})|(\d|[1-9]\d|1[0-7]\d|0{1,3})|180\.0{0,6}|180)$/');define("PHP_REG_VALIDATE_latitude",'/^(\-|\+)?([0-8]?\d{1}\.\d{0,6}|90\.0{0,6}|[0-8]?\d{1}|90)$/');define("PHP_REG_VALIDATE_DNS",'/^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?$/');define("PHP_REG_VALIDATE_DOMAIN",'/^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})*\.(com\.cn|com|net|org|info|mobi|cn)$/');define("PHP_REG_VALIDATE_NUMBERNOT0",'/^[1-9]\d*$/');define("PHP_REG_VALIDATE_NAME15",'/^[a-zA-Z0-9|-]{1,15}$/');define("PHP_REG_VALIDATE_IPRANGE",'/^(\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])-(\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])$/');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; } 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 } } 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; } 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: if(empty($val) && $val !=0) { //1:不为空 但是可以为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类型的age参数; val的array 1|2-->$_POST['age']的值不为空,且不为0,,其值符合正则,且大于等于1小于等于255; [1:不为空、不为零 2:正则、范围] $valicheck['age']=array("type"=>0,"val"=>array('1|2','/^[0-9]\d{0,2}$/',1,255)); //type:0-->post类型的ipv4参数; val的array 3-->$_POST['ipv4']的值符合ipv4格式; [3:正则,单次匹配] $valicheck['ipv4']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_IP)); $valicheck['ipv4']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_IP_1)); $valicheck['ipv4_1']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_IP_INTMASK)); $valicheck['ipv4_2']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_IP_IPMASK)); //type:0-->post类型的mask参数; val的array 3-->$_POST['mask']的值符合ipv4格式; [3:正则,单次匹配] $valicheck['mask']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_MASK)); $valicheck['mask_1']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_MASKINT)); //type:0-->post类型的ipv6参数; val的array 3-->$_POST['ipv6']的值符合ipv6格式; [3:正则,单次匹配] $valicheck['ipv6']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_IPV6)); //type:0-->post类型的ipv4-6参数; val的array 4-->$_POST['ipv4-6']的值符合ipv4格式或者ipv6格式; [4:正则,多次匹配,符合ipv4或者ipv6] $valicheck['ipv4-6']=array("type"=>0,"val"=>array('4',PHP_REG_VALIDATE_IP,PHP_REG_VALIDATE_IPV6)); //type:0-->post类型的ipv4-6参数; val的array 4-->$_POST['ipv4-6']的值符合ipv4格式或者ipv6格式; [4:正则,多次匹配,符合ipv4或者ipv6] $valicheck['mac']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_MAC)); //type:0-->post类型的port参数; val的array 3-->$_POST['port']的值符合port格式; [3:正则,单次匹配] $valicheck['port']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_PORT)); $valicheck['port_1']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_PORT_ZERO)); //type:0-->post类型的date参数; val的array 3-->$_POST['date']的值符合日期时间格式; [3:正则,单次匹配] $valicheck['date']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_DATE)); $valicheck['date_1']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_DATE_ZERO)); $valicheck['time']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_TIME)); $valicheck['datetime']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_DATETIME_ZERO)); $valicheck['datetime']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_DATETIME_ZERO_1)); $valicheck['datetime_1']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_DATETIME)); //type:0-->post类型的password参数; val的array 5-->$_POST['password']的值符合数字、字母、特殊字符中的两者,且大于等于8位小于等于10位 ; [5:函数校验,checkValidatePwdChk为指定函数,或者自定义其他函数] $valicheck['password']=array("type"=>0,"val"=>array('5','checkValidatePwdChk',8,10)); //type:0-->post类型的class参数; val的array 5-->$_POST['class']的值符合字符串可为空;不为空时,由字母、数字、下划线、中划线、点和中文组成,1-31个字符,且不能以NMC_ 开头 [5:函数校验,checkValidateNameCH为自定义其他函数] $valicheck['name']=array("type"=>0,"val"=>array('5','checkValidateNameCH')); //type:0-->post类型的select参数; val的array 6-->$_POST['select']的值在数组array('aaa','bbb','ccc')中, [6:,验证值是都被选中] $valicheck['select']=array("type"=>0,"val"=>array('6',array('aaa','bbb','ccc'))); //?????? //$valicheck['radio_one']=array("type"=>0,"mutiple_val"=>array("0"=>array('1|2'),"1"=>array('1|2'))); //type:0-->post类型的ipv4area参数; val的array 5-->$_POST['ipv4area']的值可为空、为ALL、为all、为1.1.11.1、为2.2.2.2-5.3.3.6 [5:函数校验,checkValidateAreaIp为检验ip的函数] $valicheck['ipv4area']=array("type"=>0,"val"=>array('5','checkValidateAreaIp')); $valicheck['ipv4area_1']=array("type"=>0,"val"=>array('5','checkValidatePureIp')); $valicheck['ipv4area_2']=array("type"=>0,"val"=>array('5','checkValidateIpMask')); //type:0-->post类型的ipv6area参数; val的array 5-->$_POST['ipv6area']的值可为空、为ALL、为all、为2006::19\r\n2006::9-2006::19\r\n2006:19/64 [5:函数校验,checkValidateAreaIp为检验ip的函数] $valicheck['ipv6area']=array("type"=>0,"val"=>array('5','checkValidateAreaIpv6')); //type:0-->post类型的macarea参数; val的array 5-->$_POST['macarea']的值可为空、为00:23:24:5D:58:71 [5:函数校验,checkValidateMac为检验mac的函数] $valicheck['macarea']=array("type"=>0,"val"=>array('5','checkValidateMac')); //type:0-->post类型的portarea参数; val的array 5-->$_POST['portarea']的值可为空、为1-65535之间的数 [5:函数校验,checkValidatePort为检验端口的函数] $valicheck['portarea']=array("type"=>0,"val"=>array('5','checkValidatePort')); //时间校验: 匹配 [3:正则,单次匹配] //$valicheck['time_2']=array("type"=>0,"val"=>array('3',PHP_REG_VALIDATE_TIME_2)); //type:0-->post类型的portarea参数; val的array 5-->$_POST['port']的值可为x、a-b、a-b/c-d; 0<=port<=65535的数 [5:函数校验,checkValidatePortAll为检验端口的函数] $valicheck['port_area']=array("type"=>0,"val"=>array('5',"checkValidatePortAll")); //type:0-->post类型的portarea参数; val的array 5-->$_POST['ipPort']的值可为 0<=port<=255 [5:函数校验,checkValidatePortAll为检验端口的函数] $valicheck['ipPort']=array("type"=>0,"val"=>array('5',"checkValidateIpPortAll"));ValidateCommit::validate("index.html",$valicheck);function checkValidatePwdChk($pwd,$min,$max){ if($min!=0){ if(strlen($pwd)<$min){ return false; } } if($max!=0){ if(strlen($pwd)>$max){ return false; } } $pattern= '/^[0-9a-z\~\!\@\#\$\%\^\*\&\-\+\,\.\;\|\>\<$]{8,}$/i'; // i 不区分大小写 if(preg_match($pattern,$pwd)){ $res1 = preg_match('/[0-9]+/', $pwd); $res2 = preg_match('/[A-Za-z]+/', $pwd); $res3 = preg_match('/[\~\!\@\#\$\%\^\&\*\-\+\,\.\;\|\>\<]+/', $pwd); if($res1+$res2+$res3>=2){ return true; }else{ return false; } }else{ return false; } return true;}function checkValidateSelectIn($str,$arr) { $str = trim($str); if($str == '') return true; $result = false; if(is_array($arr)) { foreach($arr as $val) { if((string)$val == (string)$str) { $result = true; break; } } } return $result;}function checkValidateNameCH($str) { if(trim($str) == '') return true; $reg = "/^[\x{4e00}-\x{9fa5}|a-zA-Z0-9|_|\-\.]{0,32}$/u"; $exceptStr = "NMC_"; if($str!=''&&(!preg_match($reg,$str)||strpos($str,$exceptStr)===0)) { return false; } else { $newStr = iconv("GBK","UTF-8",$str); if(strlen(preg_replace('/[\x{4e00}-\x{9fa5}]/u','xxx',$str)) >32) { return false; } } return true;}function checkValidateIpMask($str) { if(trim($str) == '') return true; $ipArr = explode(chr(13),$str); $exp3 = "/^(\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])$/"; $exp4 = "/^(\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|128|0))$/"; $result = true; for($i=0; $i<count($ipArr);$i++) { if($ipArr[$i] == '') continue; $ip = trim($ipArr[$i]); if(stripos($ip,'/')!==FALSE) { if(!(preg_match($exp3,$ip)||preg_match($exp4,$ip))) { $result = false; break; } } else { $result = false; break; } } return $result;}function checkValidatePureIp($str) { if(trim($str) == '') return true; if(strtolower(trim($str)) == 'all' || trim($str) == 'All') return true; $ipArr = explode(chr(13),$str); $exp1 = "/^(\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])$/"; $exp2 = "/^(\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])-(\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])$/"; $result = true; for($i=0; $i<count($ipArr);$i++) { if($ipArr[$i] == '') continue; $ip = trim($ipArr[$i]); if(stripos($ip,'-')!==FALSE) { if(!(preg_match($exp2,$ip))) { $result = false; break; } } else if(!(preg_match($exp1,$ip))) { $result = false; break; } } return $result;}function checkValidateAreaIp($str) { if(trim($str) == '') return true; if(trim($str) == 'All') return true; $ipArr = explode(chr(13),$str); $exp1 = "/^(\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])$/"; $exp2 = "/^(\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])-(\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])$/"; $exp3 = "/^(\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])$/"; $exp4 = "/^(\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|128|0))$/"; $result = true; for($i=0; $i<count($ipArr);$i++) { if($ipArr[$i] == '') continue; $ip = trim($ipArr[$i]); if(stripos($ip,'/')!==FALSE) { if(!(preg_match($exp3,$ip)||preg_match($exp4,$ip))) { $result = false; break; } } else if(stripos($ip,'-')!==FALSE) { if(!(preg_match($exp2,$ip))) { $result = false; break; } } else if(!(preg_match($exp1,$ip))) { $result = false; break; } } return $result;}function checkValidateAreaIpv6($str) {if(trim($str) == '') return true; if(strtolower(trim($str)) == 'all' || trim($str) == 'All') return true; $ipArr = explode(chr(13),$str);$rg = '/^([\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^::([\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:):([\da-fA-F]{1,4}:){0,3}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){2}:([\da-fA-F]{1,4}:){0,2}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){3}:([\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){4}:((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$|^:((:[\da-fA-F]{1,4}){1,6}|:)$|^[\da-fA-F]{1,4}:((:[\da-fA-F]{1,4}){1,5}|:)$|^([\da-fA-F]{1,4}:){2}((:[\da-fA-F]{1,4}){1,4}|:)$|^([\da-fA-F]{1,4}:){3}((:[\da-fA-F]{1,4}){1,3}|:)$|^([\da-fA-F]{1,4}:){4}((:[\da-fA-F]{1,4}){1,2}|:)$|^([\da-fA-F]{1,4}:){5}:([\da-fA-F]{1,4})?$|^([\da-fA-F]{1,4}:){6}:$/';$rg3 = '/^([0-9]+)$/'; $result = true; for($i=0; $i<count($ipArr);$i++) {if($ipArr[$i] == '') continue;$ip = trim($ipArr[$i]); if(stripos($ip,'/')!==FALSE) {$subArr = explode('/',$ip); if(!(count($subArr) == 2 && preg_match($rg,$subArr[0]) && preg_match($rg3,$subArr[1]))) { $result = false; break; } } else if(stripos($ip,'-')!==FALSE) {$subArr = explode('-',$ip); if(!(count($subArr) == 2 && preg_match($rg,$subArr[0]) && preg_match($rg,$subArr[1]))) { $result = false; break; } } else if(!(preg_match($rg,$ip))) { $result = false; break; }} return $result; }function checkValidateMac($str) { if(trim($str) == '') return true; $ipArr = explode(chr(13),$str); $exp2 = "/^([0-9a-fA-F]{2})(([\s:][0-9a-fA-F]{2}){5})$/"; $exp3 = "ff:ff:ff:ff:ff:ff"; $result = true; for($i=0; $i<count($ipArr);$i++) { if($ipArr[$i] == '') continue; $ip = trim($ipArr[$i]); if(!preg_match($exp2,$ip) || strtolower($ip) == $exp3) { $result = false; break; } } return $result;}function checkValidatePort($str) { if(trim($str) == '') return true; $ipArr = explode(chr(13),$str); $exp2 = "/^[0-9]\d{0,4}$/"; $result = true; for($i=0; $i<count($ipArr);$i++) { if($ipArr[$i] == '') continue; $ip = trim($ipArr[$i]); if(!preg_match($exp2,$ip) || $ip<1 || $ip>65535) { $result = false; break; } } return $result;}function checkValidatePortAll($str){ if(trim($str) == '') return true; //若字符串为空,返回true $rg = "/^(\d{0,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/"; $portArr = explode(chr(13), $str); $result = true; for($i=0; $i<count($portArr); $i++) { if($portArr[$i] == '') continue; $portOne = trim($portArr[$i]); if(stripos($portOne,'/')!=FALSE){ //若不等于-1,说明情况为:a-b/c-d $portTemp = explode('/', $portOne); if(stripos($portTemp[0], '-')!==FALSE)// /左側有- { $p = explode('-', $portTemp[0]); if(!(preg_match($rg, trim($p[0]))) || !(preg_match($rg, trim($p[1]))) ){ $result = false; break; } }else{ // /左側没有- if(!(preg_match($rg, trim($portTemp[0])))){ $result = false; break; } } if(stripos($portTemp[1], '-')!==FALSE) // /右側有- { $p = explode('-', $portTemp[1]); if(!(preg_match($rg, trim($p[0]))) || !(preg_match($rg, trim($p[1]))) ){ $result = false; // var_dump($result); break; } }else{ // /右側没有- if(!(preg_match($rg, trim($portTemp[1])))){ $result = false; break; } } }else if(stripos($portOne,'-')!==FALSE){ //当情况为: a-b $p = explode('-', $portOne); //含有- if(!(preg_match($rg, trim($p[0]))) || !(preg_match($rg, trim($p[1]))) ){ $result = false; break; } }else{ //当情况为: x if(!(preg_match($rg, trim($portOne)))){ $result = false; break; } } } return $result;}function checkValidateIpPortAll($str){ if(trim($str) == '') return true; //若字符串为空,返回true $rg = "/^([0-9]\d{0,2})$/"; $portArr = explode(chr(13), $str); $result = true; for($i=0; $i<count($portArr); $i++) { if($portArr[$i] == '') continue; if(!(preg_match($rg, trim($portArr[$i])))){ $result = false; break; } } return $result;}?>
* ValidateCommit::validate("index.html",array( * "name"=>array("type"=>1,"val"=>array('1')), * "radio_neGotiation"=>array("val"=>array('3','/^[0-1]$/')), * "radio_speed"=>array("val"=>array('3','/^([0-3]|\s*)$/')), * "text_mtu"=>array("val"=>array('1|2','/^[0-9]\d{2,3}$/',256,1500)), * "name"=>array("val"=>array('1|5','checkNameCH'))* ));
1、type字段 0.$_POST, 1.$_GET, 2.$_REQUST
2、val字段 1 验证为空 2 验证整数, 3 验证单个正则, 4 验证两个正则 5 验证函数 6 验证select 多个之间用数线|分割例如:$valicheck['age']=array("type"=>0,"val"=>array('1')); //age字段不可为空$valicheck['age']=array("type"=>0,"val"=>array('2','/^[0-9]\d{0,2}$/',1,255));//age值其值符合正则,且大于等于1小于等于255;$valicheck['age']=array("type"=>0,"val"=>array('3','/^[0-9]\d{0,2}$/')); //验证age符合/^[0-9]\d{0,2}$/ $valicheck['ipv4-6']=array("type"=>0,"val"=>array('4',PHP_REG_VALIDATE_IP,PHP_REG_VALIDATE_IPV6));//[4:正则,多次匹配,符合ipv4或者ipv6]$valicheck['password']=array("type"=>0,"val"=>array('5','checkValidatePwdChk',8,10));//[5:已经定义好的函数]$valicheck['name']=array("type"=>0,"val"=>array('5','checkValidateNameCH'));//[5:自定义的函数]$valicheck['select']=array("type"=>0,"val"=>array('6',array('aaa','bbb','ccc')));//[6:查看字符串是否在array内]$valicheck['age']=array("type"=>0,"val"=>array('1|2','/^[0-9]\d{0,2}$/',1,255)); //age值不为空,且不为0,,其值符合正则,且大于等于1小于等于255;
来源地址:https://blog.csdn.net/weixin_42167759/article/details/127439848
--结束END--
本文标题: php对参数校验(名称、地址、掩码、日期、时间、端口)
本文链接: https://lsjlt.com/news/433689.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0