@a5635268
2015-09-12T10:39:36.000000Z
字数 472
阅读 1390
PHP
算法
PHP算法
function code62($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;
}
function shorturl($url) {
$url = crc32($url);
$result = sprintf("%u", $url);
$shorturl = code62($result);
//插入数据库等逻辑,方便通过短网址逆差长网址
return $shorturl;
}
echo shorturl("此处为网址");
$id = 10000000; //把长网址插入数据库,获得自增长id
$url_code = base_convert($id, 10, 36); // 10000 => 7ps
$id = base_convert($url_code, 36, 10); // 7ps => 10000