>  기사  >  php教程  >  php常用正则

php常用正则

PHP中文网
PHP中文网원래의
2016-05-25 17:11:041088검색

php代码

<?php
class Verify{	
/**
 * 验证用户名
 * @param string $value
 * @param int $length
 * @return boolean
 */
	public static function isNames($value, $minLen=2, $maxLen=20, $charset=&#39;ALL&#39;){
		if(empty($value))
			return false;
		switch($charset){
			case &#39;EN&#39;: $match = &#39;/^[_\w\d]{&#39;.$minLen.&#39;,&#39;.$maxLen.&#39;}$/iu&#39;;
				break;
			case &#39;CN&#39;:$match = &#39;/^[_\x{4e00}-\x{9fa5}\d]{&#39;.$minLen.&#39;,&#39;.$maxLen.&#39;}$/iu&#39;;
				break;
			default:$match = &#39;/^[_\w\d\x{4e00}-\x{9fa5}]{&#39;.$minLen.&#39;,&#39;.$maxLen.&#39;}$/iu&#39;;
		}
		return preg_match($match,$value);
	}
	
	/**
	 * 验证密码
	 * @param string $value
	 * @param int $length
	 * @return boolean
	 */
	public static function isPWD($value,$minLen=5,$maxLen=16){
		$match=&#39;/^[\\~!@#$%^&*()-_=+|{}\[\],.?\/:;\&#39;\"\d\w]{&#39;.$minLen.&#39;,&#39;.$maxLen.&#39;}$/&#39;;
		$v = trim($value);
		if(empty($v)) 
			return false;
		return preg_match($match,$v);
	}
	
	/**
	 * 验证eamil
	 * @param string $value
	 * @param int $length
	 * @return boolean
	 */
	public static function isEmail($value,$match=&#39;/^[\w\d]+[\w\d-.]*@[\w\d-.]+\.[\w\d]{2,10}$/i&#39;){
		$v = trim($value);
		if(empty($v)) 
			return false;
		return preg_match($match,$v);
	}
	
	/**
	 * 验证电话号码
	 * @param string $value
	 * @return boolean
	 */
	public static function isTelephone($value,$match=&#39;/^0[0-9]{2,3}[-]?\d{7,8}$/&#39;){
		$v = trim($value);
		if(empty($v)) 
			return false;
		return preg_match($match,$v);
	}
	
	/**
	 * 验证手机
	 * @param string $value
	 * @param string $match
	 * @return boolean
	 */
	public static function isMobile($value,$match=&#39;/^[(86)|0]?(13\d{9})|(15\d{9})|(18\d{9})$/&#39;){
		$v = trim($value);
		if(empty($v)) 
			return false;
		return preg_match($match,$v);
	}
	/**
	 * 验证邮政编码
	 * @param string $value
	 * @param string $match
	 * @return boolean
	 */
	public static function isPostcode($value,$match=&#39;/\d{6}/&#39;){
		$v = trim($value);
		if(empty($v)) 
			return false;
		return preg_match($match,$v);
	}
	/**
	 * 验证IP
	 * @param string $value
	 * @param string $match
	 * @return boolean
	 */
	public static function isIP($value,$match=&#39;/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/&#39;){
		$v = trim($value);
		if(empty($v))
			return false;
		return preg_match($match,$v);
	}
	
	/**
	 * 验证身份证号码
	 * @param string $value
	 * @param string $match
	 * @return boolean
	 */
	public static function isIDcard($value,$match=&#39;/^\d{6}((1[89])|(2\d))\d{2}((0\d)|(1[0-2]))((3[01])|([0-2]\d))\d{3}(\d|X)$/i&#39;){
		$v = trim($value);
		if(empty($v)) 
			return false;
		else if(strlen($v)>18) 
			return false;
		return preg_match($match,$v);
	}
	
	/**
	 * *
	 * 验证URL
	 * @param string $value
	 * @param string $match
	 * @return boolean
	 */
	public static function isURL($value,$match=&#39;/^(http:\/\/)?(https:\/\/)?([\w\d-]+\.)+[\w-]+(\/[\d\w-.\/?%&=]*)?$/&#39;){
		$v = strtolower(trim($value));
		if(empty($v)) 
			return false;
		return preg_match($match,$v);	
	}
}
?>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.