项目中经常使用到的验证,很使用的。查看与下载
<?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. ES6 类

    ES6之前没有类的概念,一般采用以下方式来模仿类 基本的类声明语法 私有属性是实例中的属性,不会出现在原型上,且只能在类的构造函数中创建所有私有属性 PersonClass声明实际上创建了一个具有构造 ...

  2. SparkStreaming “Could not read data from write ahead log record” 报错分析解决

    # if open wal org.apache.spark.SparkException: Could not read data from write ahead log record FileB ...

  3. HTML5 学习04—— MathML数学标记

    MathML 元素标签: <math>...</math> MathML 是数学标记语言,是一种基于XML(标准通用标记语言的子集)的标准,用来在互联网上书写数学符号和公式的置 ...

  4. 也谈谈我对Docker的简单理解

    Docker能解决什么问题呢?一个工具的出现必然需要解决一些问题,Docker也不例外,简单说说我们常见的2种情况Docker是如何解决的吧.1.程序在我这跑得好好的,在你那怎么就不行呢?!这是一个典 ...

  5. Java Web 清除缓存

    res.setHeader("Cache-Control", "no-cache"); res.setHeader("Pragma", &q ...

  6. 这13个开源GIS软件,你了解几个?【转】

    泰伯网有看点的空间地理信息资讯都在这,你还在等什么? 这些开源GIS软件,你了解几个?本文内容部分来源于一份罗列了关于GIS软件应用的文章,笔者将其编译整合. 地理信息系统(Geographic In ...

  7. Python的ctypes 和pyinstaller

    这几天在学习python的爬虫, 无意中看到一篇博文 Python爬虫之自制英汉字典 发现里面的ctypes 和pyinstaller 还不了解,这边文章说白了就是你输入英文, python读取你的输 ...

  8. Glide4 用法总结 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  9. centos cron 自动执行脚本异常 命令不生效的解决办法

    办法: 1.sh脚本加入 source /etc/profile 2.非系统命令,要写绝对路径

  10. Elasticsearch集群运维

    一.索引管理 1. 创建索引 PUT test-2019-03 { "settings": { "index": { "number_of_shard ...