PHP简单模拟thinkphp的大I方法进行参数过滤验证

//GET/POST参数获取
function I($key,$def='',$func=''){
	$val = null;
	if(isset($_GET[$key])){
		$val = $_GET[$key];
	}
	if(isset($_POST[$key])){
		$val = $_POST[$key];
	}
	//post可以覆盖get
	if($val === null){
		return $def;
	}
	if($func == ''){
		//默认过滤
		return htmlspecialchars(trim($val));
	}else if(substr($func,0,1) == '/'){
		//正则
		try{
			if(preg_match($func,$val)){
				return $val;
			}
			return $def;
		}catch(Exception $e){
			//throw new Exception('regular expression error');
			return $def;
		}
	}else if($func == '-'){
		//不做过滤处理
		return trim($val);
	}else{
		//函数
		$arr = explode(',',$func);
		foreach($arr as $one){
			if(function_exists($one)){
				$val = call_user_func($one,$val);
			}
		}
		return $val;
	}
}