PHP generates a short URL of 3 ways code instance _php instance

Source: Internet
Author: User

Short web site services, may be a lot of friends are no longer unfamiliar, and now most of the microblogging, mobile mail reminders and other places have a lot of application mode, and occupy a certain market. It is estimated that many friends are using it now. Read Sina's short connection service, found behind the main 6 string composition.

Too many algorithmic things, there is no need to explore too much, the most important or implementation, the following are three methods of code:

<?php//Pure random generation method function random ($length, $pool = ') {$random = '; 
      if (empty ($pool)) {$pool = ' abcdefghkmnpqrstuvwxyz '; 
    $pool. = ' 23456789 '; 
 
    Srand (Double) microtime () *1000000); 
    for ($i = 0; $i < $length; $i + +) {$random. = substr ($pool, (rand ()% (strlen ($pool)), 1); 
  return $random;
} $a =random (6);  
 
Print_r ($a);  Enumeration Generation method function Shorturl ($input) {$base = Array ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "s", "T", "U", "V", "w", "X", "Y", "Z" "," A "," B "," C "," D "," E "," F "," G "," H "," I "," J "," K "," L "," M "," N "," O "," P "," Q "," R "," S "," T "," U "," V "," W 
 
 "," X "," Y "," Z "); 
 $hex = MD5 ($input); 
 $hexLen = strlen ($hex); 
 $subHexLen = $hexLen/8; 
 
 $output = Array (); 
  for ($i = 0; $i < $subHexLen; $i + +) {$subHex = substr ($hex, $i * 8, 8); $int = 0x3ffFFFFF & (1 * (' 0x '; $subHex)); 
 
  $out = '; 
   for ($j = 0; $j < 6; $j + +) {$val = 0x0000001f & $int; 
   $out. = $base 32[$val]; 
  $int = $int >> 5; 
 $output [] = $out; 
return $output;
} $a =shorturl ("http://www.jb51.net");
Print_r ($a); 
 
 62-bit Generation method function Base62 ($x) {$show = '; 
 
while ($x > 0) {$s = $x% 62;       
 
if ($s >) {$s = Chr ($s +61); 
 
} elseif ($s > 9 && $s <=35) {$s = Chr ($s + 55); 
 
 $show. = $s; 
 
$x = Floor ($x/62);   
 
return $show; 
 
The function Urlshort ($url) {$url = CRC32 ($url); 
 
$result = sprintf ("%u", $url); 
 
 Return base62 ($result); 
 
Echo urlshort ("http://www.jb51.net/");
 ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.