Location: PHPKode > projects > litephp > litephp-master/lib/input.class.php
<?php
/**
 * input:php格式检查类
 * 用法:
 * require_once('../src/input.class.php');
	$arr=array(
	array('name','username'),
		array('email','email'),
		array('tel','notempty')
	);
	$i=new input;
	$data=$i->create($arr);
	if(!$data){
		echo $i->error_field;
	}else{
		print_r($data);
	}
 **/
class input
{
	public $error_field='';
	public function create($arr,$m='post')
	{
		$M=($m==='post')?$_POST:$_GET;
		foreach($arr as $var){
			if(method_exists($this,$var[1])){
				if(isset($M[$var[0]]) and $this->$var[1]($M[$var[0]])){}
				else{
					$this->error_field=$var[0];
					return false;
				}
			}else{
				if(isset($M[$var[0]]) and eval('return '.str_replace('$'.$var[0],'\''.$M[$var[0]].'\'',$var[1]).';')){}
				else{
					$this->error_field=$var[0];
					return false;
				}
			}
		}
		return $M;
	}

	public function notempty($str){
		return !empty($str);
	}
	public function email($str){
		return is_string($str)&&preg_match('/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$/',$str);
	}
	public function url($str){
		return is_string($str)&&preg_match("/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/",$str);
	}
	public function phone($str){
		return preg_match('/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/',$str);
	}
	public function ip($ip){
		return preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" . "(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $ip);
	}
	public function date($var){
		return preg_match("/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/",$var);
	}
	public function color($var){
		return preg_match("/^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$/",$var);
	}
	public function username($var){
		return preg_match("/^[a-zA-Z0-9_\.\-]{3,16}$/",$var);
	}
	public function pic($var){
		return preg_match("/^[a-zA-Z0-9\-\.]+\.(jpg|jpeg|gif|png)$/",$var);
	}
	public function tel($str)
	{
		return preg_match("/^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$/",$str);
	}
}
Return current item: litephp