Home  >  Article  >  Backend Development  >  Simple code for php to generate short URL

Simple code for php to generate short URL

WBOY
WBOYOriginal
2016-07-25 08:54:48726browse
  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. }
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn