Home >Backend Development >PHP Tutorial >PHP generates short URL, imitates Weibo short URL generation code

PHP generates short URL, imitates Weibo short URL generation code

WBOY
WBOYOriginal
2016-07-25 08:54:471025browse
  1. urlShort-php generated Short URL_bbs.it-home.org_Script Academy provides
Copy the code

2 to generate the code for the PHP short URL.

  1. header("Content-Type:text/html;charset=UTF-8");
  2. function base62($x){
  3. $show = '';
  4. while($x> ;0){
  5. $s = $x % 62;
  6. if ($s > 35){
  7. $s = chr($s + 61);
  8. }else if ($s > 5 && $S<= 35){
  9. $s = chr($s + 55);
  10. }
  11. $show .= $s;
  12. $x = floor($x/62);
  13. }
  14. return $show;
  15. }
  16. function url_short ($url){
  17. $url = crc32($url);
  18. $result = sprintf("%u",$url);
  19. return base62($result);
  20. }
  21. echo ("The generated short URL is :".url_short($_POST['url'])."");
Copy code


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