PHP常用表达式用法

PHP常用表达式用法

1.匹配正整数:/^[1-9]\d*$/

2.匹配非负整数(正整数+0):/^\d+$/

3.匹配中文:/^[\x{4e00}-\x{9fa5}]+$/u

4.匹配Email:/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/

5.匹配网址URL:(((f|ht){1}(tp|tps)://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)

6.匹配字母开头,5-16字符,字母数字下划线:/^[a-zA-Z][a-zA-Z0-9_]{4,15}$/

7.匹配数字,字母,下划线,中文:/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u

8.匹配中国邮政编码:/^[1-9]\d{5}$/

9.匹配IP地址:/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/

10.匹配中国大陆身份证:/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$/

PHP正则验证字符串方法举例

<?php

$str = "中文啊";
 $preg = "/^[\x{4e00}-\x{9fa5}]+$/u"; //匹配中文
 if(preg_match($preg,$str,$arr))
                                                {   
                                        $msg = '匹配成功!'; 
                                                 }
else
       {
             $msg = '匹配失败!'; 
        } 
echo $msg; 


?>

 

posted @ 2016-01-25 16:00  D(a/e)mon  阅读(398)  评论(0编辑  收藏  举报