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

use*_*431 1 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;
}
Run Code Online (Sandbox Code Playgroud)

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

Gor*_*ran 9

这是我以前为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--;
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

简单地使用:

$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.
Run Code Online (Sandbox Code Playgroud)

  • 有没有理由对此进行投票?我付出了一些努力将其从c ++转换为php.虽然我可以看到这是一个非常类似的问题答案,但有一些修改可以提高效率(例如字符串连接很昂贵.) (4认同)

小智 5

只是为了完成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++;
    }
}
Run Code Online (Sandbox Code Playgroud)

该呼叫与Goran的呼叫完全相同:

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

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

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

echo $strBase."<br>"; //e7w
echo $intReturn; //123456
Run Code Online (Sandbox Code Playgroud)