当前位置:Gxlcms > PHP教程 > php生成短网址的简单代码

php生成短网址的简单代码

时间:2021-07-01 10:21:17 帮助过:14人阅读

  1. /*
  2. * php短网址生成
  3. * edit: bbs.it-home.org
  4. */
  5. class Base62{
  6. private static $base62 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  7. public static function encode($number, $encode = ''){
  8. while($number > 0){
  9. $mod = bcmod($number, 62);
  10. $encode .= self::$base62[$mod];
  11. $number = bcdiv(bcsub($number, $mod), 62);
  12. }
  13. return strrev($encode);
  14. }
  15. public static function decode($encode, $number = 0){
  16. $length = strlen($encode);
  17. $baselist = array_flip(str_split(self::$base62));
  18. for($i = 0; $i < $length; $i++){
  19. $number = bcadd($number, bcmul($baselist[$encode[$i]], bcpow(62, $length - $i - 1)));
  20. }
  21. return $number;
  22. }
  23. }

人气教程排行