项目中经常使用到的验证,很使用的。查看与下载
<?php
/**
* 验证类
*
* @lastmodify 2015-12-19
* @author wuheng
*/
class Verify{
/**
* 是否为空值
*/
public static function isEmpty($str){
$str = trim($str);
return !empty($str) ? true : false;
}
/**
* 数字验证
* param:$flag : int是否是整数,float是否是浮点型
*/
public static function isNum($str,$flag = 'float'){
if(!self::isEmpty($str)) return false;
if(strtolower($flag) == 'int'){
return ((string)(int)$str === (string)$str) ? true : false;
}else{
return ((string)(float)$str === (string)$str) ? true : false;
}
}
/**
* 名称匹配,如用户名,目录名等
* @param:string $str 要匹配的字符串
* @param:$chinese 是否支持中文,默认支持,如果是匹配文件名,建议关闭此项(false)
* @param:$charset 编码(默认utf-8,支持gb2312)
*/
public static function isName($str,$chinese = true,$charset = 'utf-8'){
if(!self::isEmpty($str)) return false;
if($chinese){
$match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_-]+$/" : "/^[x{4e00}-x{9fa5}A-Za-z0-9_]+$/u";
}else{
$match = '/^[A-za-z0-9_-]+$/';
}
return preg_match($match,$str) ? true : false;
}
/**
* 邮箱验证
*/
public static function isEmail($str){
if(!self::isEmpty($str)) return false;
return preg_match("/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i",$str) ? true : false;
}
//手机号码验证
public static function isMobile($str){
$exp = "/^1[3|4|5|7|8][0-9]{9}$/";
if(preg_match($exp,$str)){
return true;
}else{
return false;
}
}
/**
* URL验证,纯网址格式,不支持IP验证
*/
public static function isUrl($str){
if(!self::isEmpty($str)) return false;
return preg_match('#(http|https|ftp|ftps)://([w-]+.)+[w-]+(/[w-./?%&=]*)?#i',$str) ? true : false;
}
/**
* 验证中文
* @param:string $str 要匹配的字符串
* @param:$charset 编码(默认utf-8,支持gb2312)
*/
public static function isChinese($str,$charset = 'utf-8'){
if(!self::isEmpty($str)) return false;
$match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."]+$/"
: "/^[x{4e00}-x{9fa5}]+$/u";
return preg_match($match,$str) ? true : false;
}
/**
* UTF-8验证
*/
public static function isUtf8($str){
if(!self::isEmpty($str)) return false;
return (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word)
== true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word)
== true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word)
== true) ? true : false;
}
/**
* 验证长度
* @param: string $str
* @param: int $type(方式,默认min <= $str <= max)
* @param: int $min,最小值;$max,最大值;
* @param: string $charset 字符
*/
public static function length($str,$type=3,$min=0,$max=0,$charset = 'utf-8'){
if(!self::isEmpty($str)) return false;
$len = mb_strlen($str,$charset);
switch($type){
case 1: //只匹配最小值
return ($len >= $min) ? true : false;
break;
case 2: //只匹配最大值
return ($max >= $len) ? true : false;
break;
default: //min <= $str <= max
return (($min <= $len) && ($len <= $max)) ? true : false;
}
}
/**
* 验证密码
* @param string $value
* @param int $length
* @return boolean
*/
public static function isPWD($value,$minLen=6,$maxLen=16){
$match='/^[\\~!@#$%^&*()-_=+|{}\[\],.?\/:;\'\"\d\w]{'.$minLen.','.$maxLen.'}$/';
$v = trim($value);
if(empty($v))
return false;
return preg_match($match,$v);
}
/**
* 验证用户名
* @param string $value
* @param int $length
* @return boolean
*/
public static function isNames($value, $minLen=2, $maxLen=16, $charset='ALL'){
if(empty($value))
return false;
switch($charset){
case 'EN': $match = '/^[_\w\d]{'.$minLen.','.$maxLen.'}$/iu';
break;
case 'CN':$match = '/^[_\x{4e00}-\x{9fa5}\d]{'.$minLen.','.$maxLen.'}$/iu';
break;
default:$match = '/^[_\w\d\x{4e00}-\x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu';
}
return preg_match($match,$value);
}
/**
* 验证邮箱
* @param string $value
*/
public static function checkZip($str){
if(strlen($str)!=6){
return false;
}
if(substr($str,0,1)==0){
return false;
}
return true;
}
/**
* 匹配日期
* @param string $value
*/
public static function checkDate($str){
$dateArr = explode("-", $str);
if (is_numeric($dateArr[0]) && is_numeric($dateArr[1]) && is_numeric($dateArr[2])) {
if (($dateArr[0] >= 1000 && $timeArr[0] <= 10000) && ($dateArr[1] >= 0 && $dateArr[1] <= 12) && ($dateArr[2] >= 0 && $dateArr[2] <= 31))
return true;
else
return false;
}
return false;
}
/**
* 匹配时间
* @param string $value
*/
public static function checkTime($str){
$timeArr = explode(":", $str);
if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric($timeArr[2])) {
if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] <= 59))
return true;
else
return false;
}
return false;
}
}

PHP正则验证类的更多相关文章

  1. 一个PHP常用表单验证类(基于正则)

    一个基于正则表达式的PHP常用表单验证类,作者:欣然随风.这个表单判断类的功能有:验证是否为指定长度的字母/数字组合.验证是否为指定长度汉字.身 份证号码验证.是否是指定长度的数字.验证邮件地址.电话 ...

  2. iOS身份证的正则验证

    在ios项目的开发中可能很多地方都需要用到身份证校验,一般在开发的时候很多人都是直接百度去网上荡相关的正则表达式和校验代码,但是网上疯狂粘贴复制的校验代码本身也可能并不准确,可能会有风险,比如2013 ...

  3. Android常用正则工具类

    此类提供日常开发中常用的正则验证函数,比如:邮箱.手机号.电话号码.身份证号码.日期.数字.小数.URL.IP地址等.使用Pattern对象的matches方法进行整个字符匹配,调用该方法相当于:   ...

  4. crawler_工具类_RegexUtils_正则帮助类

    package com.cph.crawler.core.utils; import java.util.ArrayList; import java.util.List; import java.u ...

  5. WPF TextBox 正则验证 大于等于0 小于等于1 的两位小数

    正则:^(0\.\d+|[1-9][0-9]|1)$ TextBox绑定正则验证 <TextBox x:Name="txb"   MaxLength="6" ...

  6. Yii正则验证

    required : 必须值验证属性 [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #说明:CRequiredV ...

  7. 使用c#正则验证关键字并找出匹配项

    在.net里,使用类Regex可以正则验证一些关键字并取出匹配项. 1.使用Regex.IsMatch(string  input,  string  pattern,  RegexOptions   ...

  8. 【唯星宠物】——BootStrap/Mysql/PHP/Ajax爬坑之正则验证登录注册子页

    前言:唯星宠物产品官网的登录注册,单独一个子页,页面使用BootStrap实现响应式,PHP提供服务端,利用Ajax技术,从Mysql中获取JSON数据,并对表单信息进行正则验证.项目github地址 ...

  9. android经常使用正则工具类

    此类提供日常开发中经常使用的正则验证函数.比方:邮箱.手机号.电话号码.身份证号码.日期.数字.小数.URL.IP地址等.使用Pattern对象的matches方法进行整个字符匹配,调用该方法相当于: ...

随机推荐

  1. Docker安装管理界面portainer

    在Ubuntu或者Debian已经部署完毕Docker 拉取镜像文件: sudo docker pull docker.io/portainer/portainer Using default tag ...

  2. Python中将array类型不按科学计数法存在文件中的方法

    直接上代码: from numpy import *import numpy as npDrug_array = zeros((708,708),dtype = int)f = open('D:\ma ...

  3. IIS7常见错误及解决方法

    IIS7常见错误及解决方法   问题一:HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效.  详细错误信息模块 IIS We ...

  4. Quartz Cron表达式 每周、每月执行一次

    原文:https://blog.csdn.net/qq_33432559/article/details/75633767 系统中通常有些需要自动执行的任务,这些任务可能每隔一段时间就要执行一次,也可 ...

  5. 咏南APP(手机)开发框架

    咏南APP(手机)开发框架 有意者可向咏南索取DEMO. 基于DELPHI官方的FIREMONKEY类库构建,不使用任何三方控件. 原生手机框架,支持各种手机硬件操作. 主界面 聊天 照相并分享 短信 ...

  6. 用GO开发企业级分布式云存储系统

    一.基础架构 二.开发工具

  7. OHDSI——数据标准化

    Home › Data Standardization Data Standardization Data standardization is the critical process of bri ...

  8. JavaScript中Object值合并方法

    原文:https://www.cnblogs.com/fullstack-yang/p/8085206.html ------------------------------------ 前言:在日常 ...

  9. 终于考完PMP

    快到年底,上手了GO语言,搞定了广告后台,觉得年底前应该不忙; 我们的PM也报了PMP,可以一起报团学习,后来由于地理位置关系,我们项目组,三个人报了不同的班,也好,信息可以共享; 跟我们公司合作的培 ...

  10. 【JavaScript从入门到精通】第三课 初探JavaScript魅力-03

    第三课 初探JavaScript魅力-03 函数传参 上节课的时候我们已经讲了什么是函数,实际上,函数在功能上就类似于css的class一样,将一段代码包裹起来使用.为了让函数的功能更加的丰富和实用, ...