zoukankan      html  css  js  c++  java
  • 验证身份证信息类

       /**
         * 验证身份证号是否正确
         * @param type $idCard 身份证号
         * @return boolean 返回判断结果
         * @date : 2017-04-07
         */
        public function _isIdCard($id=0) {
            $id = strtoupper($id);
            $regx = "/(^d{15}$)|(^d{17}([0-9]|X)$)/";
            $arr_split = array();
            if (!preg_match($regx, $id)) {
                return FALSE;
            }
            if (15 == strlen($id)) { //检查15位 
                $regx = "/^(d{6})+(d{2})+(d{2})+(d{2})+(d{3})$/";
    
                @preg_match($regx, $id, $arr_split);
                //检查生日日期是否正确 
                $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
                if (!strtotime($dtm_birth)) {
                    return FALSE;
                } else {
                    return TRUE;
                }
            } else {      //检查18位 
                $regx = "/^(d{6})+(d{4})+(d{2})+(d{2})+(d{3})([0-9]|X)$/";
                @preg_match($regx, $id, $arr_split);
                $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
                if (!strtotime($dtm_birth)) { //检查生日日期是否正确 
                    return FALSE;
                } else {
                    //检验18位身份证的校验码是否正确。 
                    //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。 
                    $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                    $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                    $sign = 0;
                    for ($i = 0; $i < 17; $i++) {
                        $b = (int) $id{$i};
                        $w = $arr_int[$i];
                        $sign += $b * $w;
                    }
                    $n = $sign % 11;
                    $val_num = $arr_ch[$n];
                    if ($val_num != substr($id, 17, 1)) {
                        return FALSE;
                    } else {
                        return TRUE;
                    }
                }
            }
        }
        /**
         * 当前时间的毫秒戳
         * @return type
         * @date : 2017-04-07
         */
        public function _msectime() {
            $arr = explode(' ', microtime());
            $tmp1 = $arr[0];
            $tmp2 = $arr[1];
            return (float) sprintf('%.0f', (floatval($tmp1) + floatval($tmp2)) * 1000);
        }
    
        /**
         * 十进制转62进制
         * @param type $dec
         * @return type
         * @date : 2017-04-07
         */
        public function _dec62($dec) {
            $base = 62;
            $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $ret = '';
            for ($t = floor(log10($dec) / log10($base)); $t >= 0; $t--) {
                $a = floor($dec / pow($base, $t));
                $ret .= substr($chars, $a, 1);
                $dec -= $a * pow($base, $t);
            }
            return $ret;
        }
    
        /**
         * 生成八位随机字符
         * @return string
         * @date : 2017-04-07
         */  
        public function _randChar() {
            $base = 62;
            $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            return $chars[mt_rand(1, $base) - 1];
        }
        
            /**
         * 通过身份证号获取性别;出生日期
         * @param type $card_no 身份证号
         * @return array
         * @date : 2017-05-17
         */
        public function _getSexByCard($card_no){
            $userSexInfo = [];
            $birth = strlen($card_no)==15 ? ('19' . substr($card_no, 6, 6)) : substr($card_no, 6, 8);
            $sex = substr($card_no, (strlen($card_no)==15 ? -2 : -2), 1);
            if($sex % 2 === 0){
               $sex = 2; 
            }else{
               $sex = 1;  
            }
            $userSexInfo['birth'] = $birth;
            $userSexInfo['sex'] = $sex;
            return $userSexInfo;
        }
        
             /**
         * @uses 根据生日计算年龄,年龄的格式是:2016-09-23
         * @param string $birthday
         * @return string|number
         * @date : 2017-04-26
         */
        public function calcAge($birthday = '') {
            $iage = 0;
            if (!empty($birthday)) {
                $year = date('Y', strtotime($birthday));
                $month = date('m', strtotime($birthday));
                $day = date('d', strtotime($birthday));
    
                $now_year = date('Y');
                $now_month = date('m');
                $now_day = date('d');
    
                if ($now_year > $year) {
                    $iage = $now_year - $year - 1;
                    if ($now_month > $month) {
                        $iage++;
                    } else if ($now_month == $month) {
                        if ($now_day >= $day) {
                            $iage++;
                        }
                    }
                }
            }
            return $iage;
        }
  • 相关阅读:
    C++ MFC学习 (二)
    C++ MFC字符转换
    C++ MFC学习 (一)
    Windows.h 文件学习
    Git 学习
    Git 学习
    php压缩文件夹并下载到本地
    接口类型无限级分类
    mysql 共享锁 排它锁
    docker基础命令
  • 原文地址:https://www.cnblogs.com/ghjbk/p/7809409.html
Copyright © 2011-2022 走看看