php 常用验证类及正则

正则表达式在遇到新的时候将会不断更新

<?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');


 



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值