Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GeHou/d4cf3fe89f33c709c3657d4225c8099b to your computer and use it in GitHub Desktop.
Save GeHou/d4cf3fe89f33c709c3657d4225c8099b to your computer and use it in GitHub Desktop.
/**
* 获取该条记录的自增ID
* 将自增转换为62进制,并拼接网址 如:http://qetee.com/w7e
* 用户访问到 http://qetee.com/w7e 时,提取短网址后缀 w7e
* 将短网址后缀转换为10进制,得到自增ID号 如:123456
* 使用查询该记录,进行业务逻辑处理(比如跳转)
*/
/**
* 十进制数转换成62进制
*
* @param integer $num
* @return string
*/
function from10_to62($num) {
$to = 62;
$dict = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$ret = '';
do {
$ret = $dict[bcmod($num, $to)] . $ret;
$num = bcdiv($num, $to);
} while ($num > 0);
return $ret;
}
/**
* 62进制数转换成十进制数
*
* @param string $num
* @return string
*/
function from62_to10($num) {
$from = 62;
$num = strval($num);
$dict = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$len = strlen($num);
$dec = 0;
for($i = 0; $i < $len; $i++) {
$pos = strpos($dict, $num[$i]);
$dec = bcadd(bcmul(bcpow($from, $len - $i - 1), $pos), $dec);
}
return $dec;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment