zoukankan      html  css  js  c++  java
  • 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地址:/(?:(?: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]?)/

    10.匹配中国大陆身份证:/^[1-9]d{5}[1-9]d{3}((0d)|(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; 
    
    
    ?>
  • 相关阅读:
    python 数据类型
    python核心语法
    python 基础知识
    format 用法
    有关python 函数参数
    模拟,队列与堆栈
    字符编码
    [LeetCode] Set Matrix Zeroes
    [LeetCode] Rotate Image
    [LeetCode] Unique Paths
  • 原文地址:https://www.cnblogs.com/fangchongyan/p/5157730.html
Copyright © 2011-2022 走看看