PHP shortURL

<?php

   $table ="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$table = str_shuffle($table);
//$table = "6Qw7fWXgTjkcordIKLvBstRSD3n904U5e8ZMOPluhJmiNxVyzY12AFGHabpCEq";  //映射表

$e = _10to62("159046587215904658721590465872159046587215904658721590465872", $table);
var_dump($e);
$d = _62to10($e, $table);
var_dump($d);



/**
 * 将十进制转成62进制
 * @param $n
 */
function _10to62($n,$table){

    $res = "";
    while(($a=bcdiv($n, 62)) > 0)
    {
        $res = $table[bcsub($n, bcmul(62, $a))].$res;
        $n = $a;
    }
    $res = $table[bcsub($n, bcmul(62, $a))].$res;
    return $res;
}
 
/**
 * 将 62进制转成10进制
 */
 function _62to10($m,$table){

    $res = 0;
    for($n=strlen($m),$i=$n-1;$i>=0;$i--){
        $res = bcadd($res, bcmul(strpos($table,$m[$n-$i-1]), bcpow(62, $i)));
    }
    return $res;
}

 

PHP shortURL

上一篇:6-1 函数基础


下一篇:C语言学习第三天