如何将一个非常大的数字压缩成字母数字(在PHP中)?

时间:2013-11-23 08:49:31

标签: php

我想将一个非常大的数字压缩成字母数字[0-9a-zA-Z]。 当然,最简单的方法是使用一个名为'base64_encode()'的内置php函数,但我完全贬低了这个方法,它会产生额外的字符,如'/'和'='。更重要的是,base64_encode无法压缩数字,因为此函数将数字视为字符串。

我曾考虑过另一个名为'base_convert()'的in-bulid函数,但它可以将数字转换为charset [0-9a-z],从而使结果更长。

我现在用一种廉价的方式来实现我的目标:     

function compress_int($num) {
    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    $result = '';

    while( $num ) {
        $mod = $num % 52;
        $num = intval($num / 52);

        $result .= $chars[$mod];
    }

    return $result;
}

但是,我认为效率很低。所以我非常感谢能够以更高的效率告诉我更好的方法。 ^ _ ^

2 个答案:

答案 0 :(得分:8)

这是我之前为c ++应用程序做的一个。随意使用它。

// $num - the number we want to convert
// $symbols - the chars you want to use e.g. '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
// &$out is a pointer to your $result
function intToBase($num, $symbols, &$out){

    // get the radix that we are working with
    $radix = strlen($symbols);

    $pos = strlen($out)-1;

    if($num==0){
        // if our number is zero then we can just use the first character of our symbols and we are done.
        $out[$pos] = $symbols[0];
    }else{
        // otherwise we have to loop through and rebase the integer one character at a time.
        while ($num > 0) {
            // split off one digit
           $r = $num % $radix;
            // convert it and add it to the char array
            $out[pos] = $symbols[r];
            // subtract what we have added to the compressed string
            $num = ($num - $r) / $radix;
            $pos--;
        }
    }
};

简单使用:

$num = 123004954712; //whatever number you want to compress
$result = "";// the result variable we will be writing to
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';// the caracters of our custom base.
intToBase($num, $chars, $result);// the call
// now our $result variable will have the rebased string.

答案 1 :(得分:4)

只是为了完成Goran的答案并节省某人的时间,这就是转换后获取int值的功能:

function baseToInt($base, $symbols, &$out) {
    //get length of the char map, so you can change according to your needs
    $radix = strlen($symbols);

    //split the chars into an array and initialize variables
    $arr = str_split($base,1);
    $i = 0;
    $out = 0;

    //loop through each char assigning values
    //chars to the left are the least significant
    foreach($arr as $char) {
        $pos = strpos($symbols, $char);
        $partialSum = $pos * pow($radix, $i);
        $out += $partialSum;
        $i++;
    }
}

电话与Goran的电话完全相同:

$number = 123456;
$symbols = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

$strBase = "";
intToBase($number, $symbols, $strBase);

$intReturn = 0;
baseToInt($strBase, symbols , $intReturn);

echo $strBase."<br>"; //e7w
echo $intReturn; //123456