正则表达式在遇到新的时候将会不断更新
<?php
class Validation
{
private static function getRexp($rexp)
{
$_rexp = array (
'letter_number'=>'/^[0-9A-Za-z]+$/',//只有字母数字包括大小写
'letter_number_'=>'/^[0-9A-Za-z_]+$/',//只有字母数字下划线包括大小写
'ids'=>'/^[0-9]+(\,[0-9]+)*$/',//验证多个id以','分割的类型 例如'1,2,3,4,5'
'ids_negative'=>'/^-?[0-9]+(\,-?[0-9]+)*$/',//验证多个id以','分割的类型 例如'-1,2,3,4,5'
'linearCodes'=>'/^[0-9A-Z_]+(\,[0-9A-Z_]+)*$/',//验证多个id以','分割的类型 例如'1,2,3,4,5'
'orderby'=>'/^[0-9A-Za-z_]+(\,[0-9A-Za-z_]+)*$/',//验证多个id以','分割的类型 例如'1,2,3,4,5'
'number'=>'/^[0-9]+$/',//只可以使数字
'float'=>'/^[0-9]*(\\.?)[0-9]*/', //浮点型
'personal_card'=>'/^[0-9A-Za-z]+$/',//身份证
'email'=>'/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/',//邮箱
'date'=>'/^((((19|20)\d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$/'//日期,包含了闰年
);
if (isset($_rexp[$rexp])) {
return $_rexp[$rexp];
} else {
return $rexp_not_defind;
}
}
private static function exitValidate ($invalidateKey)
{
$httpStatus = 'HTTP/1.1 1400 ' . $invalidateKey . ' is not validate';
header($httpStatus);
header('apiversion:1.0.0');//默认版本号1.0.0
header('Content-type: application/json');
header('appcookie: ' . 'appcookie001');
exit();
}
private static function doAddslashes ($data, $addslashes=true)
{
if (is_array($data))
{
foreach($data as $k=>$v)
{
if(empty($data[$k])){
continue;
}
if (is_array($data[$k]))
{
$data[$k] = self::doAddslashes($data[$k], $addslashes);
} else
{
//json格式的数据不进行转义
if ($addslashes || is_null(json_decode($data[$k])))
{
$data[$k] = addslashes($v);
}
}
}
}
return $data;
}
/**
* 数组校验通过返回校验后的数据 不通过直接退出程序返回错误码并返回未通过的参数
*/
public static function validate($data, $config, $deleteUselessKey = false, $addslashes = true, $decodeType = true)
{
if(empty($data))
{
self::exitValidate('null');
}
if (!is_array($data))
{
$data = json_decode($data, true);
//json解析失败
if (!$data)
{
self::exitValidate('json structure');
}
}
$_config = ValidateParameterConfig::getConfig($config);
//确定配置文件中需要穿的参数都存在
$k = self::allowExist($data, $_config);
if ($k !== null) {
self::exitValidate($k);
}
if ($deleteUselessKey == true)
{
$data = self::deleteUselessKey($data, $_config);
}
//递归转义
$data = self::doAddslashes($data, $addslashes);
//校验
foreach($_config as $k=>$c) {
if (isset($data[$k]))
{
if(isset($c['rexp']))
{
if (self::vRexp( $data[$k], $c['rexp']) === false) {
self::exitValidate($k);
}
}
if (isset($c['length']))
{
if (self::vLength($data[$k], $c['length']))
{
self::exitValidate($k);
}
}
if (isset($c['min_length']))
{
if (!self::vMinLength($data[$k], $c['min_length']))
{
self::exitValidate($k);
}
}
if (isset($c['max_length']))
{
if (!self::vMaxLength($data[$k], $c['max_length']))
{
self::exitValidate($k);
}
}
}
}
return $data;
}
private static function allowExist($data, $config)
{
foreach ($config as $k=>$v)
{
if (!isset($v['allow_exist']) || $v['allow_exist'] == true) {
if (!isset($data[$k]))
{
return $k;
}
}
}
return null;
}
public static function deleteUselessKey($data, $config)
{
if (is_array($config))
{
$_config = $config;
} else
{
$_config = ValidateParameterConfig::getConfig($config);
}
foreach((array)$data as $k=>$v)
{
if (!isset($_config[$k]))
{
unset($data[$k]);
}
}
return $data;
}
public static function issetUselessValue($data, $arr)
{
foreach($data as $k=>$v)
{
if (!in_array($v, $arr))
{
return false;
}
}
return true;
}
public static function vRexp($data, $rexp) {
$_rexp = self::getRexp($rexp);
if (preg_match($_rexp, $data) == false) {
return false;
}
return true;
}
public static function vLength($data, $l) {
if (strlen(trim($data)) == $l) {
return false;
}
return true;
}
public static function vMinLength($data, $l) {
if (strlen(trim($data)) < $l) {
return false;
}
return true;
}
public static function vMaxLength($data, $l) {
if (strlen(trim($data)) > $l) {
return false;
}
return true;
}
public static function vLetterNumber($data) {
if (preg_match(self::getRexp('letter_number'), $data) == false) {
return false;
}
return true;
}
public static function vLetterNumber_($data) {
if (preg_match(self::getRexp('letter_number_'), $data) == false) {
return false;
}
return true;
}
public static function vNumber($data) {
if (preg_match(self::getRexp('number'), $data) == false) {
return false;
}
return true;
}
public static function vEmail($data) {
if (preg_match(self::getRexp('email'), $data) == false) {
return false;
}
return true;
}
}
使用例子:
从应用端发过来的参数为
$arr = array('letter_number'=>'abc123', 'account'=>'acb1234_');
//参数验证
$arr = Validation::validate($arr, 'test');