


PHP implements the method of regular regular verification helper public class
这篇文章主要介绍了PHP实现的常规正则验证helper公共类,结合完整实例形式分析了php针对常规的电话、手机、邮箱、账号等进行正则验证的操作技巧,需要的朋友可以参考下
主要代码功能: 弥补平时项目对于验证功能这块的不严谨。具体细分的常规验证, 手机号/电话/小灵通验证, 字符串长度区间合法验证, 邮箱验证, 使用正则验证数据.
/** * * * 常规验证helper公共类 * * */ class CheckForm { //手机号/电话/小灵通 验证 public function Mobile_check($mobile,$type = array()) { /** * 手机号码 * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188 * 联通:130,131,132,152,155,156,185,186 * 电信:133,1349,153,180,189 */ $res[1]= preg_match('/^1(3[0-9]|5[0-35-9]|8[0-9])\\d{8}$/', $mobile); /** * 中国移动:China Mobile 11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188 */ $res[2]= preg_match('/^1(34[0-8]|(3[5-9]|5[017-9]|8[0-9])\\d)\\d{7}$/', $mobile); /** * 中国联通:China Unicom * 130,131,132,152,155,156,185,186 */ $res[3]= preg_match('/^1(3[0-2]|5[256]|8[56])\\d{8}$/', $mobile); /** * 中国电信:China Telecom * 133,1349,153,180,189 */ $res[4]= preg_match('/^1((33|53|8[09])[0-9]|349)\\d{7}$/', $mobile); /** * 大陆地区固话及小灵通 * 区号:010,020,021,022,023,024,025,027,028,029 * 号码:七位或八位 */ $res[5]= preg_match('/^0(10|2[0-5789]|\\d{3})-\\d{7,8}$/', $mobile); $type = empty($type) ? array(1,2,3,4,5) : $type; $ok = false; foreach ($type as $key=>$val) { if ($res[$val]) { $ok = true; } continue; } if ( $mobile && $ok ) { return true; } else{ return false; } } //字符串长度区间合法验证 public function Strlength_check($str, $min=NULL, $max=NULL) { preg_match_all("/./u", $str, $matches); $len = count($matches[0]); if(is_null($min) && !empty($max) && $len < $max){ return false; } if(is_null($max) && !empty($min) && $len > $min){ return false; } if ($len < $min || $len > $max) { return false; } return true; } //邮箱验证 public static function isEmail($str) { if (!$str) { return false; } return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $str) ? true : false; } /** * 使用正则验证数据 * @access public * @param string $rule 验证规则 * @param string $value 要验证的数据 * @return boolean */ public function regex($rule,$value) { $validate = array( //字段必须,不能为空 'require' => '/\S+/', //邮箱验证 'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', //url验证 'url' => '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/', //货币验证 'currency' => '/^\d+(\.\d{0,2})?$/', //数字验证 'number' => '/^[-\+]?\d+(\.\d+)?$/', //zip验证 'zip' => '/^\d{6}$/', //整数验证 'integer' => '/^[-\+]?\d+$/', //浮点数验证 'double' => '/^[-\+]?\d+(\.\d+)?$/', //英文验证 'english' => '/^[A-Za-z]+$/', 'gt0' => '/^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/', //合法帐号 'account' => '/^[a-zA-Z][a-zA-Z0-9_]{1,19}$/' ); // 检查是否有内置的正则表达式 if(isset($validate[strtolower($rule)])) $rule = $validate[strtolower($rule)]; return preg_match($rule,$value)===1; } function CheckPwd($pwd,$min=NULL, $max=NULL) { if (strlen($pwd)>$max || strlen($pwd)<$min || preg_match("/^\d*$/",$pwd) || preg_match("/^[a-z]*$/i",$pwd)) { return false; } return true; } }
is_null()
检测变量是否为 NULL。
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
The above is the detailed content of PHP implements the method of regular regular verification helper public class. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
