Home  >  Article  >  Backend Development  >  PHP generates short URL sample code

PHP generates short URL sample code

WBOY
WBOYOriginal
2016-07-25 08:54:39823browse
  1. function base62($x)
  2. {
  3. $show = '';
  4. while($x > 0) {
  5. $s = $x % 62;
  6. if ($s > ; 35) {
  7. $s = chr($s+61);
  8. } elseif ($s > 9 && $s <=35) {
  9. $s = chr($s + 55);
  10. }
  11. $ show .= $s;
  12. $x = floor($x/62);
  13. }
  14. return $show;
  15. }
  16. function urlShort($url)
  17. {
  18. $url = crc32($url);
  19. $result = sprintf("%u", $url);
  20. return base62($result);
  21. }
  22. echo urlShort("http://code.google.com/p/rfphp4zf");
Copy code

The PHP short URL generation code shared above, I hope it will be helpful to everyone.



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