快乐在线

  • 首页
  • AI技术
  • 代码分享
  • 友情链接
  • 文章归档
  • 关于

PHP各种数据类型验证库

  • 哇哈哈
  • 2025-04-22
  • 0
<?php
// +----------------------------------------------------------------------
// | h-admin
// +----------------------------------------------------------------------
// | 验证助手库
// +----------------------------------------------------------------------
// | Author: waiszer <waiszer@163.com>
// +----------------------------------------------------------------------

declare(strict_types = 1);

namespace app\common\helper;

use app\common\traits\Error;

class Test
{

    use Error;

    /**
     * 验证手机号
     * @param string|null $str
     * @return bool
     */
    public static function isMobile(?string $str = null) :bool
    {
        if($str && preg_match("/^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/u", $str)) {
            return true;
        }
        self::setStaticError('请输入正确的手机号!');
        return false;
    }

    /**
     * 验证邮箱
     * @param string|null $str
     * @return bool
     */
    public static function isEmail(?string $str = null) :bool
    {
        if($str && preg_match("/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/u", $str)) {
            return true;
        }
        self::setStaticError('请输入正确的邮箱号!');
        return false;
    }

    /**
     * 验证电话
     * @param string|null $str
     * @return bool
     */
    public static function isTel(?string $str = null) :bool
    {
        if($str && preg_match("/^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$/u", $str)) {
            return true;
        }
        self::setStaticError('请输入正确的座机电话!');
        return false;
    }

    /**
     * 验证身份证号
     * 大陆 1代、2代
     * 香港
     * 澳门
     * 台湾
     * @param string|null $str
     * @return bool
     */
    public static function isIdCard(?string $str = null) :bool
    {
        // 大陆
        $rule = "/(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0[1-9]|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/u";
        // 香港
        $xg_rule = "/^[a-zA-Z]\d{6}\([\dA]\)$/u";
        // 台湾
        $tw_rule = "/^[a-zA-Z][0-9]{9}$/u";
        // 澳门
        $am_rule = "/^[1|5|7]\d{6}[(\d)]{3}$/u";
        if($str && (preg_match($rule, $str) || preg_match($xg_rule, $str) || preg_match($tw_rule, $str) || preg_match($am_rule, $str))) {
            return true;
        }

        self::setStaticError('请输入正确的身份证号!');
        return false;
    }

    /**
     * 检查是否是md5值
     * @param string|null $md5
     * @return bool
     */
    public static function isMd5(?string $md5 = null) :bool
    {
        if (preg_match("/^[a-z0-9]{32}$/u", $md5)) {
            return true;
        }
        self::setStaticError('请输入正确的MD5字符串!');
        return false;
    }

    /**
     * 网址(url,支持端口和"?+参数"和"#+参数)
     * @param string|null $str
     * @return bool
     */
    public static function isUrl(?string $str = null) :bool
    {
        if (preg_match('/^(((ht|f)tps?):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?$/u', $str)) {
            return true;
        }
        self::setStaticError('请输入正确的URL!');
        return false;
    }

    /**
     * 判断字符串是否为json字符串
     * @param string|null $str
     * @return bool
     */
    public static function isJsonStr(?string $str = null): bool
    {
        if (is_array($str) || empty($str) || is_numeric($str)) {
            self::setStaticError('请传入正确的json字符串!');
            return false;
        }
        if (!is_scalar($str) && !is_null($str) && !method_exists($str, '__toString')) {
            self::setStaticError('请传入正确的json字符串!');
            return false;
        }

        json_decode($str);

        if (json_last_error() === JSON_ERROR_NONE) {
            return true;
        }
        self::setStaticError('请传入正确的json字符串!');
        return false;
//        return !empty($str) && ((json_decode($str, true) ? true : false));
    }

    /**
     * 验证邮编(中国)
     * @param string|null $str
     * @return bool
     */
    public static function isPostCode(?string $str = null): bool
    {
        if (preg_match('/^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$/', $str)) {
            return true;
        }
        self::setStaticError('请传入正确的邮编!');
        return false;
    }

    /**
     * 验证车牌号
     * @param string|null $str
     * @return bool
     */
    public static function isCarNumber(?string $str = null): bool
    {
        if (preg_match('/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/', $str)) {
            return true;
        }
        self::setStaticError('请传入正确的车牌号!');
        return false;
    }

    /**
     * 验证中文姓名
     * @param string|null $str
     * @return bool
     */
    public static function isChineseName(?string $str = null): bool
    {
        if (preg_match('/^[\u4E00-\u9FA5·]{2,16}$/', $str)) {
            return true;
        }
        self::setStaticError('请输入正确的中文姓名!');
        return false;
    }

    /**
     * 银行卡号(10到30位, 覆盖对公/私账户, 参考[微信支付](https://pay.weixin.qq.com/wiki/doc/api/xiaowei.php?chapter=22_1))
     * @param string|null $str
     * @return bool
     */
    public static function isBankNumber(?string $str = null): bool
    {
        if (preg_match('/^[1-9]\d{9,29}$/', $str)) {
            return true;
        }
        self::setStaticError('请输入正确的银行卡号!');
        return false;
    }

    /**
     * 数字/货币金额(支持负数、千分位分隔符)
     * @param string|null $str
     * @return bool
     */
    public static function isAmount(?string $str = null): bool
    {
        if (preg_match('/^-?\d{1,3}(,\d{3})*(\.\d{1,2})?$/', $str)) {
            return true;
        }
        self::setStaticError('请输入正确的金额!');
        return false;
    }

    /**
     * 统一社会信用代码
     * @param string|null $str
     * @return bool
     */
    public static function isUsci(?string $str = null): bool
    {
        if (preg_match('/^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/', $str)) {
            return true;
        }
        self::setStaticError('请输入正确的社会统一信用代码!');
        return false;
    }

    /**
     * 验证密码
     * 只能以字母(a-zA-Z)开头、长度不低于6位,必须包含数字(0-9)、特殊符号(任意):~、!、@、#、¥、%、&、*、(、)、-、=、{、}、[、]、!
     * @param string|null $value
     * @param int $min
     * @return bool
     */
    public static function isPassword(?string $string = null, int $min = 6): bool
    {
        if (preg_match('/^[a-zA-Z](?=.*[0-9])(?=.*[~!@#¥%&*()\-=\+_{}\[\]{}])(?!.*[\/\\\\]).{'.($min - 1).',}$/u', $string)) {
            return true;
        }
        self::setStaticError('密码只能以字母(a-zA-Z)开头、长度不低于6位,必须包含数字(0-9)、特殊符号(任意):~、!、@、#、¥、%、&、*、(、)、-、_、=、{、}、[、] 组合!');
        return false;
    }

    /**
     * 验证账号长度与合法性
     * 默认 帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线组合
     * @param string|null $str
     * @param int $min
     * @param int $max
     * @return bool
     */
    public static function isAccount(?string $str = null, int $min = 5, int $max = 16): bool
    {
        if (preg_match('/^[a-zA-Z][a-zA-Z0-9_]{'.($min - 1).','.($max - 1).'}$/i', $str)) {
            return true;
        }
        self::setStaticError('账号只能以字母开头,允许'.$min.'-'.$max.'个字符,允许字母数字下划线(_)组合!');
        return false;
    }
}
© 2025 快乐在线
Theme by Wing
  • {{ item.name }}
  • {{ item.name }}