php常用验证

这个也是从网上找的,没有验证过是否正确,使用时需要小心。

<?php
class class_post {
	//验证是否为指定长度的字母/数字组合 
	function fun_text1($num1, $num2, $str) {
		Return (preg_match ( "/^[a-zA-Z0-9]{" . $num1 . "," . $num2 . "}$/", $str )) ? true : false;
	}
	//验证是否为指定长度数字 
	function fun_text2($num1, $num2, $str) {
		return (preg_match ( "/^[0-9]{" . $num1 . "," . $num2 . "}$/i", $str )) ? true : false;
	}
	//验证是否为指定长度汉字 
	function fun_font($num1, $num2, $str) {
		// preg_match("/^[\xa0-\xff]{1,4}$/", $string); 
		return (preg_match ( "/^([\x81-\xfe][\x40-\xfe]){" . $num1 . "," . $num2 . "}$/", $str )) ? true : false;
	}
	//验证身份证号码 
	function fun_status($str) {
		return (preg_match ( '/(^([\d]{15}|[\d]{18}|[\d]{17}x)$)/', $str )) ? true : false;
	}
	//验证邮件地址 
	function fun_email($str) {
		return (preg_match ( '/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$/', $str )) ? true : false;
	}
	//验证电话号码 
	function fun_phone($str) {
		return (preg_match ( "/^(((d{3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,7}$/", $str )) ? true : false;
	}
	//验证邮编 
	function fun_zip($str) {
		return (preg_match ( "/^[1-9]d{5}$/", $str )) ? true : false;
	}
	// 数据入库 转义 特殊字符 传入值可为字符串 或 一维数组 
	function data_join(&$data) {
		if (get_magic_quotes_gpc () == false) {
			if (is_array ( $data )) {
				foreach ( $data as $k => $v ) {
					$data [$k] = addslashes ( $v );
				}
			} else {
				$data = addslashes ( $data );
			}
		}
		Return $data;
	}
	//验证邮编 
	function fun_zip($str) {
		return (preg_match ( "/^[1-9]d{5}$/", $str )) ? true : false;
	}
}
?>

Related posts:

1 thought on “php常用验证”

  1. function valid_email($address)
    {
    return ( ! preg_match(“/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix”, $address)) ? FALSE : TRUE;
    }

Leave a Reply

Your email address will not be published.