本案例共有上述三个文件
首先展示核心文件regexTool.class.php ,该文件即为此次编写的正则表达式工具类
代码如下:
<?php //php常用正则表达式工具类 class regexTool { private $validate = array(//常用的匹配规则 'require' => '/.+/', 'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', 'url' => '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/', 'currency' => '/^\d+(\.\d+)?$/', 'number' => '/^\d+$/', 'zip' => '/^\d{6}$/', 'integer' => '/^[-\+]?\d+$/', 'double' => '/^[-\+]?\d+(\.\d+)?$/', 'english' => '/^[A-Za-z]+$/', 'qq' => '/^\d{5,11}$/', 'mobile' => '/^1(3|4|5|7|8)\d{9}$/', ); private $returnMatchResult = false;//用于切换匹配的结果用,有2中结果可供选择,bool值或匹配到的目标字符串 private $fixMode = null;//修正模式 private $matches = array();//匹配的结果数组 private $isMatch = false;//验证的结果 public function __construct($returnMatchResult = false, $fixMode = null) {//构造方法 $this->returnMatchResult = $returnMatchResult; $this->fixMode = $fixMode; } //$pattern为验证规则,$subject为输入字符串。 private function regex($pattern, $subject) { if(array_key_exists(strtolower($pattern), $this->validate)) $pattern = $this->validate[$pattern].$this->fixMode; $this->returnMatchResult ? preg_match_all($pattern, $subject, $this->matches) : $this->isMatch = preg_match($pattern, $subject) === 1; return $this->getRegexResult(); } private function getRegexResult() {//用于返回匹配的结果有2种情况 if($this->returnMatchResult) { return $this->matches; } else { return $this->isMatch; } } public function toggleReturnType($bool = null) {//切换返回的类型 if(empty($bool)) { $this->returnMatchResult = !$this->returnMatchResult; } else { $this->returnMatchResult = is_bool($bool) ? $bool : (bool)$bool; } } public function setFixMode($fixMode) { $this->fixMode = $fixMode; } public function noEmpty($str) { return $this->regex('require', $str); } public function isEmail($email) { return $this->regex('email', $email); } public function isMobile($mobile) { return $this->regex('mobile', $mobile); } public function check($pattern, $subject) { return $this->regex($pattern, $subject); } //...... }
接着展示reg.html文件的代码如下:
<html> <head> <title>用户注册</title> <meta charset="utf-8"> </head> <body> <form action="regCheck.php" method="post"> 用户名 <input type="text" name="username" id="username" value="" /><br /><br /> email <input type="text" name="email" id="email" value="" /><br /><br /> 手机号 <input type="text" name="mobile" id="mobile" value="" /><br /><br /> 自定义<input type="text" name="check" id="check" value="" /><br/><br/> <input type="submit" value="注册"/> </form> </body> </html>
接着展示regCheck.php的代码,如下:
<?php header("Content-Type:text/html;charset=utf-8;");//header头声明编码方式为utf-8 require_once 'regexTool.class.php'; $regex = new regexTool(); if(!$regex->noEmpty($_POST['username'])) exit('用户名是必须填写的!'); if(!$regex->isEmail($_POST['email'])) exit('email格式错误!'); if(!$regex->isMobile($_POST['mobile'])) exit('手机号格式错误!'); if(!$regex->check("/[abcd]123/",$_POST['check'])) exit('特殊验证格式不符合'); //数据库写入 /* * @name : show * @description : output debug * @param $var : input data * @return void */ function show($var = null, $isdump = false) { $func = $isdump ? 'var_dump' : 'print_r'; if(empty($var)) { echo 'null'; } elseif(is_array($var) || is_object($var)) { //array,object echo '<pre>'; $func($var); echo '</pre>'; } else { //string,int,float... $func($var); } }
运行reg.html文件,程序运行界面如下;
如果什么都不填写就点击注册会收到如下提示:
<?php//php常用正则表达式工具类class regexTool {private $validate = array(//常用的匹配规则'require' => '/.+/','email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/','url' => '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/','currency' => '/^\d+(\.\d+)?$/','number' => '/^\d+$/','zip' => '/^\d{6}$/','integer' => '/^[-\+]?\d+$/','double' => '/^[-\+]?\d+(\.\d+)?$/','english' => '/^[A-Za-z]+$/','qq'=>'/^\d{5,11}$/','mobile'=>'/^1(3|4|5|7|8)\d{9}$/',);private $returnMatchResult = false;//用于切换匹配的结果用,有2中结果可供选择,bool值或匹配到的目标字符串private $fixMode = null;//修正模式private $matches = array();//匹配的结果数组private $isMatch = false;//验证的结果public function __construct($returnMatchResult = false, $fixMode = null) {//构造方法$this->returnMatchResult = $returnMatchResult;$this->fixMode = $fixMode;}//$pattern为验证规则,$subject为输入字符串。 private function regex($pattern, $subject) {if(array_key_exists(strtolower($pattern), $this->validate))$pattern = $this->validate[$pattern].$this->fixMode;$this->returnMatchResult ?preg_match_all($pattern, $subject, $this->matches) :$this->isMatch = preg_match($pattern, $subject) === 1;return $this->getRegexResult();}private function getRegexResult() {//用于返回匹配的结果有2种情况if($this->returnMatchResult) {return $this->matches;} else {return $this->isMatch;}}public function toggleReturnType($bool = null) {//切换返回的类型if(empty($bool)) {$this->returnMatchResult = !$this->returnMatchResult;} else {$this->returnMatchResult = is_bool($bool) ? $bool : (bool)$bool;}}public function setFixMode($fixMode) {$this->fixMode = $fixMode;}public function noEmpty($str) {return $this->regex('require', $str);}public function isEmail($email) {return $this->regex('email', $email);}public function isMobile($mobile) {return $this->regex('mobile', $mobile);}public function check($pattern, $subject) {return $this->regex($pattern, $subject);}//......}