欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php生成短网址的简单代码

程序员文章站 2022-05-08 15:48:13
...
  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 $number = bcadd($number, bcmul($baselist[$encode[$i]], bcpow(62, $length - $i - 1)));
  19. }
  20. return $number;
  21. }
  22. }
复制代码