美文网首页
php经常用到的函数

php经常用到的函数

作者: geeooooz | 来源:发表于2022-07-18 18:50 被阅读0次
    //身份证验证 start
    function validation_filter_id_card($id_card){
        if(strlen($id_card)==18){
            return idcard_checksum18($id_card);
        }elseif((strlen($id_card)==15)){
            $id_card=idcard_15to18($id_card);
            return idcard_checksum18($id_card);
        }else{
            return false;
        }
    }
    // 计算身份证校验码,根据国家标准GB 11643-1999
    function idcard_verify_number($idcard_base){
        if(strlen($idcard_base)!=17){
            return false;
        }
        //加权因子
        $factor=array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);
        //校验码对应值
        $verify_number_list=array('1','0','X','9','8','7','6','5','4','3','2');
        $checksum=0;
        for($i=0;$i<strlen($idcard_base);$i++){
            $checksum += substr($idcard_base,$i,1) * $factor[$i];
        }
        $mod=$checksum % 11;
        $verify_number=$verify_number_list[$mod];
        return $verify_number;
    }
    // 将15位身份证升级到18位
    function idcard_15to18($idcard){
        if(strlen($idcard)!=15){
            return false;
        }else{
            // 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码
            if(array_search(substr($idcard,12,3),array('996','997','998','999')) !== false){
                $idcard=substr($idcard,0,6).'18'.substr($idcard,6,9);
            }else{
                $idcard=substr($idcard,0,6).'19'.substr($idcard,6,9);
            }
        }
        $idcard=$idcard.idcard_verify_number($idcard);
        return $idcard;
    }
    // 18位身份证校验码有效性检查
    function idcard_checksum18($idcard){
        if(strlen($idcard)!=18){
            return false;
        }
        $idcard_base=substr($idcard,0,17);
        if(idcard_verify_number($idcard_base)!=strtoupper(substr($idcard,17,1))){
            return false;
        }else{
            return true;
        }
    }
    //身份证验证 end
    
    /**
     * 获取一段字符串的长度
     * 区分中文和英文
     * 无论一个汉字还是一个英文字母都只算一个字符
     * @param $str
     * @param string $charset
     */
    function strLength($str,$charset='utf-8'){
        if($charset=='utf-8') $str = iconv('utf-8','gb2312',$str);
        $num = strlen($str);
        $cnNum = 0;
        for($i=0;$i<$num;$i++){
            if(ord(substr($str,$i+1,1))>127){
                $cnNum++;
                $i++;
            }
        }
        $enNum = $num-($cnNum*2);
        $number = ($enNum/2)+$cnNum;
        return ceil($number);
    }
    
    
    /**
     * php显示指定长度的字符串,超出长度以省略号(...)填补尾部显示
     * @ str 字符串
     * @ len 指定长度
     **/
    function cutSubstr($str,$len=30){
        if (strlen($str)>$len) {
            $str=substr($str,0,$len) . '...';
        }
        return $str;
    }
    
    // 过滤掉emoji表情
    function filterEmoji($str)
    {
        $str = preg_replace_callback(
            '/./u',
            function (array $match) {
                return strlen($match[0]) >= 4 ? '' : $match[0];
            },
            $str);
        
        return $str;
    }
    
    
    /**
     * 获取毫秒级时间戳
     */
    function getMillisecond(){
        list($t1, $t2) = explode(' ', microtime());
        return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
    }
    /**
     * 随机字符串生成
     * @param int $len 生成的字符串长度
     * @return string
     */
    function sp_random_string($len = 6) {
        $chars = array(
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
            "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
            "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
            "3", "4", "5", "6", "7", "8", "9"
        );
        $charsLen = count($chars) - 1;
        shuffle($chars);    // 将数组打乱
        $output = "";
        for ($i = 0; $i < $len; $i++) {
            $output .= $chars[mt_rand(0, $charsLen)];
        }
        return $output;
    }
    
    /**
     * 发送HTTP请求方法,目前只支持CURL发送请求
     * @param  string $url    请求URL
     * @param  array  $param  GET参数数组
     * @param  array  $data   POST的数据,GET请求时该参数无效
     * @param  string $method 请求方法GET/POST
     * @return array          响应数据
     */
    function httpCurl($url, $param, $data = '', $method = 'GET'){
        $opts = array(
                CURLOPT_TIMEOUT        => 30,
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_SSL_VERIFYHOST => false,
        );
        /* 根据请求类型设置特定参数 */
        $opts[CURLOPT_URL] = $url . '?' . http_build_query($param);
        if(strtoupper($method) == 'POST'){
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $data;
    
            if(is_string($data)){ //发送JSON数据
                $opts[CURLOPT_HTTPHEADER] = array(
                        'Content-Type: application/json; charset=utf-8',
                        'Content-Length: ' . strlen($data),
                );
            }
        }
        /* 初始化并执行curl请求 */
        $ch = curl_init();
        curl_setopt_array($ch, $opts);
        $data  = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        return  $data;
    }
    
    /**
     *
     * 中英混合字符串长度判断
     * @param unknown_type $str
     * @param unknown_type $charset
     */
    function strLength($str, $charset = 'utf-8') {
        if ($charset == 'utf-8')
            $str = iconv ( 'utf-8', 'gb2312', $str );
            $num = strlen ( $str );
            $cnNum = 0;
            for($i = 0; $i < $num; $i ++) {
                if (ord ( substr ( $str, $i + 1, 1 ) ) > 127) {
                    $cnNum ++;
                    $i ++;
                }
            }
            $enNum = $num - ($cnNum * 2);
            $number = ($enNum / 2) + $cnNum;
            return ceil ( $number );
    }
    
    /**
     * Notes:
     * User: wujiawei
     * DateTime: 2022/7/26 20:01
     * describe: 计算两个日期相差 年 月 日
     * @param $date1
     * @param $date2
     * @return array
     */
    function DiffDate($date1, $date2) {
        if (strtotime($date1) > strtotime($date2)) {
            $ymd = $date2;
            $date2 = $date1;
            $date1 = $ymd;
        }
        list($y1, $m1, $d1) = explode('-', $date1);
        list($y2, $m2, $d2) = explode('-', $date2);
        $y = $m = $d = $_m = 0;
        $math = ($y2 - $y1) * 12 + $m2 - $m1;
        $y = round($math / 12);
        $m = intval($math % 12);
        $d = (mktime(0, 0, 0, $m2, $d2, $y2) - mktime(0, 0, 0, $m2, $d1, $y2)) / 86400;
        if ($d < 0) {
            $m -= 1;
            $d += date('j', mktime(0, 0, 0, $m2, 0, $y2));
        }
        $m < 0 && $y -= 1;
        return array($y, $m, $d);
    }
    
    /**
     * Notes:
     * User: wujiawei
     * DateTime: 2022/7/27 9:40
     * describe: 根据身份证号获取年龄
     * @param $idcard
     * @return false|float|int|null
     */
    function get_age($idcard)
    {
        if (empty($idcard)) return false;
        #  获得出生年月日的时间戳
        $date = strtotime(substr($idcard, 6, 8));
        #  获得今日的时间戳
        $today = strtotime('today');
        #  得到两个日期相差的大体年数
        $diff = floor(($today - $date) / 86400 / 365);
        #  strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比
        $age = strtotime(substr($idcard, 6, 8) . ' +' . $diff . 'years') > $today ? ($diff + 1) : $diff;
        return $age;
    }
    

    相关文章

      网友评论

          本文标题:php经常用到的函数

          本文链接:https://www.haomeiwen.com/subject/ojydirtx.html