zhphp framework (十八) php 验证类
2015-12-11 16:11
381 查看
<?php /** * Created by PhpStorm. * User: zhanghua * Date: 14-2-5 * Time: 23:28 * QQ: 746502560qq.com */ final class validate { /** * 验证数字 * 参数: 字符串 长度 * 说明:validate_d($string,$length) 该函数依据传递的字符和长度来验证 是否为数字 长度是否符合 */ public static function validate_d($string = null, $length = null) { $pattern = (!empty($length)) ? '|\d{' . $length . '}|' : '|\d|'; $tmp = preg_match_all($pattern, trim($string), $matchAll); if ($tmp) { return true; } else { return false; } } /** * 验证数字字母下划线 * 参数:字符串 长度 */ public static function validate_w($string = null, $length = null) { $pattern = (!empty($length)) ? '|\w{' . $length . '}|' : '|\w|'; $tmp = preg_match_all($pattern, trim($string), $mattchAll); if ($tmp) { return true; } else { return false; } } /** * 验证email * 参数:字符串 */ public static function validate_Email($email = null) { $pattern = '/(\w+[-._]?\w+)+(@)([a-zA-Z0-9]+\.)+(com|cn|net|com.cn)?/'; $tmp = preg_match_all($pattern, trim($email), $mattchAll); if ($tmp) { return true; } else { return false; } } /** * 数据基础验证-是否是身份证 * * @param string $value 需要验证的值 * @return bool */ public static function validate_card($value) { $tmp = preg_match_all("/^(\d{15}|\d{17}[\dx])$/i", trim($value), $mattchAll); if ($tmp) { return true; } else { return false; } } /** * 数据基础验证-是否是中文 * 参数: string $value 需要验证的值 * 返回 bool */ public static function validate_gbk($value = null) { $tmp = preg_match_all("/^([\xE4-\xE9][\x80-\xBF][\x80-\xBF])+$/", trim($value), $mattchAll); if ($tmp) { return true; } else { return false; } } /** * 数据基础验证-是否是QQ * @param string $value 需要验证的值 * @return bool */ public static function validate_qq($value) { $tmp = preg_match_all('/^[1-9]\d{4,12}$/', trim($value), $matttchAll); if ($tmp) { return true; } else { return false; } } /** * 数据基础验证-是否是邮政编码 * * @param string $value 需要验证的值 * @return bool */ public static function validate_zip($value) { $tmp = preg_match_all('/^[1-9]\d{5}$/', trim($value), $matttchAll); if ($tmp) { return true; } else { return false; } } /** * 数据基础验证-是否是URL * @param string $value 需要验证的值 * @return bool */ public static function validate_url($value) { $tmp = preg_match_all('/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/', trim($value), $matttchAll); if ($tmp) { return true; } else { return false; } } /** * 数据基础验证-是否是移动电话 * @param string $value 需要验证的值 * @return bool */ public static function validate_phone($value) { $tmp = preg_match_all('/^((\(\d{2,3}\))|(\d{3}\-))?(13|15)\d{9}$/', trim($value), $matttchAll); if ($tmp) { return true; } else { return false; } } /** * 数据基础验证-是否是电话 * @param string $value 需要验证的值 * @return bool */ public static function validate_mobile($value) { $tmp = preg_match_all('/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/', trim($value), $matttchAll); if ($tmp) { return true; } else { return false; } } /** * 手机号码中间显示为“****” * @param $tel */ public static function tel($tel, $isecho = true) { $pattern = "/(1\d{1,2})\d\d(\d{0,3})/"; $replacement = "\$1*****\$3"; $uname = preg_replace($pattern, $replacement, $tel); if ($isecho) { echo @mb_substr($uname, 0, 15, 'utf8'); } else { return @mb_substr($uname, 0, 15, 'utf8'); } } } ?>
相关文章推荐
- zhphp framework (十八) session 抽象接口
- ThinkPHP3.1分页时,采用联表查询的处理
- zhphp framework (十七) 模型接口
- dede 相关文章的调用
- 配置文件读取类conf.class.php 单例模式应用
- 经历了一期的项目洗礼的成长
- 织梦自定义专题节点容器模板修正
- PHP下载网页图片
- php中json_decode返回数组或对象
- 我的PHP之旅
- 功能很全的PHP分页类
- ThinkPHP3.2.3完整版中对Auth.class.php的使用
- 织梦dede后台限制简略标题字数的修改
- 如何修改dede文章页上一篇下一篇"没有了"
- php读取团购api之二
- Setting up a Passive FTP Server in Windows Azure VM(ReplyCode: 227, Entering Passive Mode )
- 数字转化为人民币大写(PHP)
- php读取和保存base64编码的图片内容
- (php)生成指定个数的随机红包
- 学习PHP三周之心态分享