• 数字ID和字符串ID互相转化


    对于想隐藏真实id的需求,比如想加密userId等
    使用示例:

    	 * 不指定长度
         * AlphaIDCustom(12354);  //会将数字转换为字母。
         * AlphaIDCustom('PpQXn7COf',true);//会将字母ID转换为对应的数字。
         * 指定长度
         * AlphaIDCustom(123456,false,6);//指定生成字母ID的长度为6.
         * AlphaIDCustom('xyMSSI',ture,6);//会将字母ID转换为对应的数字.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    源码:

        /**
         * 数字ID和字符串ID互相转换
         * 如:
         * 不指定长度
         * AlphaIDCustom(12354);  //会将数字转换为字母。
         * AlphaIDCustom('PpQXn7COf',true);//会将字母ID转换为对应的数字。
         * 指定长度
         * AlphaIDCustom(123465,false,6);//指定生成字母ID的长度为6.
         * AlphaIDCustom('xyMSSI',ture,6);//会将字母ID转换为对应的数字.
         * @param $in
         * @param false $to_num 是否转成数字
         * @param false $pad_up 长度限制
         * @param null $passKey 加密
         * @return false|string
         */
        function AlphaIDCustom($in, $to_num = false, $pad_up = false, $passKey = null)
        {
    //        $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            //乱序
            $index = 'SITEQZFyWtpPjRxVYc7uL094bOA8rDCBMmgki23XJnsfHd5oqNwzhUlGKave61';
    
            if ($passKey !== null) {
    
                for ($n = 0; $n<strlen($index); $n++) {
                    $i[] = substr( $index,$n ,1);
                }
    
                $passhash = hash('sha256',$passKey);
                $passhash = (strlen($passhash) < strlen($index))
                    ? hash('sha512',$passKey)
                    : $passhash;
    
                for ($n=0; $n < strlen($index); $n++) {
                    $p[] =  substr($passhash, $n ,1);
                }
    
                array_multisort($p,  SORT_DESC, $i);
                $index = implode($i);
            }
    
            $base  = strlen($index);
    
            if ($to_num) {
                // Digital number  <<--  alphabet letter code
                $in  = strrev($in);
                $out = 0;
                $len = strlen($in) - 1;
                for ($t = 0; $t <= $len; $t++) {
                    $bcpow = bcpow($base, $len - $t);
                    $out   = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
                }
    
                if (is_numeric($pad_up)) {
                    $pad_up--;
                    if ($pad_up > 0) {
                        $out -= pow($base, $pad_up);
                    }
                }
                $out = sprintf('%F', $out);
                $out = substr($out, 0, strpos($out, '.'));
            } else {
                // Digital number  -->>  alphabet letter code
                if (is_numeric($pad_up)) {
                    $pad_up--;
                    if ($pad_up > 0) {
                        $in += pow($base, $pad_up);
                    }
                }
    
                $out = "";
                for ($t = floor(log($in, $base)); $t >= 0; $t--) {
                    $bcp = bcpow($base, $t);
                    $a   = floor($in / $bcp) % $base;
                    $out = $out . substr($index, $a, 1);
                    $in  = $in - ($a * $bcp);
                }
                $out = strrev($out); // reverse
            }
    
            return $out;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
  • 相关阅读:
    Flask学习笔记
    装饰模式与职责链模式笔记
    suging闲谈-netty 的异步非阻塞IO线程与业务线程分离
    【快捷测试模型是否可以跑通】设置一张图片的张量形式,送入自己写的模型进行测试
    Flink从入门到放弃—Stream API—常用算子(reduce)
    Http改为Https后该如何测试
    云计算基础技术
    UniApp学习
    数据结构——新手村级二叉树讲解
    UDP就一定比TCP快吗?
  • 原文地址:https://blog.csdn.net/weixin_39618723/article/details/133957977