php生成短网址的简单代码
时间:2021-07-01 10:21:17
帮助过:14人阅读
- /*
- * php短网址生成
- * edit: bbs.it-home.org
- */
- class Base62{
- private static $base62 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
-
- public static function encode($number, $encode = ''){
- while($number > 0){
- $mod = bcmod($number, 62);
- $encode .= self::$base62[$mod];
- $number = bcdiv(bcsub($number, $mod), 62);
-
- }
- return strrev($encode);
- }
-
- public static function decode($encode, $number = 0){
- $length = strlen($encode);
- $baselist = array_flip(str_split(self::$base62));
- for($i = 0; $i < $length; $i++){
- $number = bcadd($number, bcmul($baselist[$encode[$i]], bcpow(62, $length - $i - 1)));
- }
- return $number;
- }
- }
|