- substr: documentation ( source)
- strpos: documentation ( source)
- chr: documentation ( source)
- log10: documentation ( source)
- floor: documentation ( source)
- pow: documentation ( source)
<?php
class base62{
/**
* 10进制转为62进制
* @param integer $n 10进制数值
* @return string 62进制
*/
function base10to62($n) {
$base = 62;
$index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$ret = '';
for($t = floor(log10($n) / log10($base)); $t >= 0; $t --) {
$a = floor($n / pow($base, $t));
$ret .= substr($index, $a, 1);
$n -= $a * pow($base, $t);
}
return $ret;
}
/**
* 62进制转为10进制
* @param integer $n 62进制
* @return string 10进制
*/
function base62to10($s) {
$base = 62;
$index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$ret = 0;
$len = strlen($s) - 1;
for($t = 0; $t <= $len; $t ++) {
$ret += strpos($index, substr($s, $t, 1)) * pow($base, $len - $t);
}
return $ret;
}
}
$base = new base62;
echo $base->base10to62("3819313952083157");
function base62($x){
$show='';
while($x>0){
$s= $x%62;
if ($s>35) {
$s= chr($s+61);
}elseif ($s>9&&$s<=35){
$s=chr($s+55);
}
$show.=$s;
$x=floor($x/62);
}
return $show;
}
echo base62("3819313952083157");