zoukankan      html  css  js  c++  java
  • PHP常用正则验证

    验证邮箱格式

    // 验证邮箱格式
    function checkEmail($email)
    {
        if (!preg_match("/([w-]+@[w-]+.[w-]+)/", $email)) {
            return false;
        } else {
            return true;
        }
    }

    验证 URL

    // 验证 URL
    function checkWebsite($website)
    {
        if (!preg_match("/(?:(?:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/%=~_|]/i", $website)) {
            return false;
        } else {
            return true;
        }
    }

    验证中文

    function checkChinese($str)
    {
        if(preg_match('/[x7f-xff]/', $str)){
            //字符串中有中文
            if(preg_match('/^[x7f-xff]+$/', $str)){
                //字符串全是中文
                return array('code'=>2,'msg'=>'字符串全是中文');
            }else{
    //            字符串不全是中文
                return array('code'=>1,'msg'=>'字符串不全是中文');
            }
        }else{
            //字符串中没有中文
            return array('code'=>0,'msg'=>'字符串中没有中文');
        }
    
    }

    卡号验证:

    /*
    16-19 位卡号校验位采用 Luhm 校验方法计算:
    1,将未带校验位的 15 位卡号从右依次编号 1 到 15,位于奇数位号上的数字乘以 2
    2,将奇位乘积的个十位全部相加,再加上所有偶数位上的数字
    3,将加法和加上校验位能被 10 整除。
    * @param string $s
    */
    function luhm($s)
    {
        $n = 0;
        $ns = strrev($s); // 倒序
        for ($i = 0; $i < strlen($s); $i++) {
            if ($i % 2 == 0) {
                $n += $ns[$i]; // 偶数位,包含校验码
            } else {
                $t = $ns[$i] * 2;
                if ($t >= 10) {
                    $t = $t - 9;
                }
                $n += $t;
            }
        }
        return ($n % 10) == 0;
    }
  • 相关阅读:
    1、MyBatis 快速入门
    1、Spring Framework入门
    1、基本数据类型
    2020软件工程个人作业06
    谭山明月有秋水--事后诸葛亮
    谭山明月有秋水——项冲刺第七天
    谭山明月有秋水——冲刺第六天
    谭山明月有秋水——冲刺第一天
    谭山明月有秋水——冲刺第二天
    谭山明月有秋水——冲刺第三天
  • 原文地址:https://www.cnblogs.com/jxl1996/p/10139014.html
Copyright © 2011-2022 走看看