正則表達式一直以來是我比較頭痛的東西,不過工作中是離不開正則表達式的,代碼、vim編輯器、awk等Linux命令都廣泛應用正則表達式,這是我收集并且測試過的PHP代碼中常用的正則表達式,首先建立測試函數,代碼如下:
- function regTest( $pattern, $str ) {
- var_dump( preg_match($pattern, $str) );
- preg_match_all($pattern,$str,$matches);
- var_dump( $matches );
- var_dump( preg_replace($pattern,$str,'Test') );
- }
1.匹配中文字符,代碼如下:
- $pattern = "/[x{4e00}-x{9fa5}]/u";
- $str = "飛晏-feiyan";
- regTest( $pattern, $str );
- //依次輸出結果
- //int(1)
- //array(1) { [0]=> array(2) { [0]=> string(3) "飛" [1]=> string(3) "晏" } }
- //string(28) "TestTest-feiyan"
關于漢字的匹配,網上很多給的都是“[u4e00-u9fa5]”,這個正則不一定完全正確.
2.匹配tab縮進、空格和換行,代碼如下:
- $pattern = "/[x{4e00}-x{9fa5}]/u";
- $str = "飛晏-feiyan";
- regTest( $pattern, $str );
- //依次輸出結果
- //int(1)
- //array(1) { [0]=> array(2) { [0]=> string(3) "飛" [1]=> string(3) "晏" } }
- //string(28) "TestTest-feiyan"
- //string(21) "HelloTestTest,TestPHP"
3.匹配Email地址,代碼如下:
- $pattern = "[w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*]";
- $str = '如有問題,請聯系[email protected]或者[email protected]。';
- regTest( $pattern, $str );
- //依次輸出結果
- //int(1)
- //array(4) { [0]=> array(2) { [0]=> string(18) "[email protected]" [1]=> string(15) "[email protected]" } [1]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } [2]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } [3]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } }
- //string(41) "如有問題,請聯系Test或者Test。"
- //使用Filter函數
- filter_var($email, FILTER_VALIDATE_EMAIL);
我一般不自己寫正則表達式去驗證郵箱,使用PHP內置filter函數可以很方便的完成郵箱地址的驗證.
4.匹配國內手機號碼和電話號碼,代碼如下:
- //固定電話匹配
- $pattern = "[d{3,4}-d{7,8}]";
- $str = '聯系電話010-12345678';
- regTest( $pattern, $str );
- //最簡單的匹配手機號匹配
- $pattern = "[1d{10}]";
- $str = '聯系電話15812345678';
- regTest( $pattern, $str );
5.匹配HTML中的圖片地址,代碼如下:
- $pattern = '/<[img|IMG].*?src=['|"](.*?(?:[.gif|.jpg]))['|"].*?[/]?>/';
- $str = '<img id="test_img" src="images/123.gif" alt="test" />';
- regTest( $pattern, $str );
- /**
- * 驗證郵政編碼
- * @param string $value
- * @param string $match
- * @return boolean
- */
- public static function isPostcode($value,$match='/d{6}/'){
- $v = trim($value);
- if(emptyempty($v))
- return false;
- return preg_match($match,$v);
- }
- /**
- * 驗證IP
- * @param string $value
- * @param string $match
- * @return boolean
- */
- public static function isIP($value,$match='/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/'){
- $v = trim($value);
- if(emptyempty($v))
- return false;
- return preg_match($match,$v);
- }
- /**
- * 驗證身份證號碼
- * @param string $value
- * @param string $match
- * @return boolean
- */
- public static function isIDcard($value,$match='/^d{6}((1[89])|(2d))d{2}((0d)|(1[0-2]))((3[01])|([0-2]d))d{3}(d|X)$/i'){
- $v = trim($value);
- if(emptyempty($v))
- return false;
- else if(strlen($v)>18)
- return false;
- return preg_match($match,$v);
- }
- /**
- * *
- * 驗證URLwww.111cn.net
- * @param string $value
- * @param string $match
- * @return boolean
- */
- public static function isURL($value,$match='/^(http://)?(https://)?([wd-]+.)+[w-]+(/[dw-./?%&=]*)?$/'){
- $v = strtolower(trim($value));
- if(emptyempty($v))
- return false;
- return preg_match($match,$v);
- }
表單驗證匹配
驗證賬號,字母開頭,允許 5-16 字節,允許字母數字下劃線:^[a-zA-Z][a-zA-Z0-9_]{4,15}$
驗證賬號,不能為空,不能有空格,只能是英文字母:^S+[a-z A-Z]$
驗證賬號,不能有空格,不能非數字:^d+$
驗證用戶密碼,以字母開頭,長度在 6-18 之間:^[a-zA-Z]w{5,17}$
驗證是否含有 ^%&',;=?$ 等字符:[^%&',;=?$x22]+
匹配Email地址:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
匹配騰訊QQ號:[1-9][0-9]{4,}
匹配日期,只能是 2004-10-22 格式:^d{4}-d{1,2}-d{1,2}$
匹配國內電話號碼:^d{3}-d{8}|d{4}-d{7,8}$
評注:匹配形式如 010-12345678 或 0571-12345678 或 0831-1234567
匹配中國郵政編碼:^[1-9]d{5}(?!d)$
匹配身份證:d{14}(d{4}|(d{3}[xX])|d{1})
評注:中國的身份證為 15 位或 18 位
不能為空且二十字節以上:^[s|S]{20,}$
新聞熱點
疑難解答