php 兩種短網址產生方法

來源:互聯網
上載者:User

標籤:md5   www   cti   ffffff   bsp   代碼   rtu   als   printf   

使用以下PHP代碼可以產生唯一的6位的短網址。

代碼如下:

 1 <?php 2  3  4 //產生短網址方法1 5 function shortUrl1($url) 6 { 7     if (empty($url)) { 8         return FALSE; 9     }10     $url   = crc32($url);11     $crc32 = sprintf("%u", $url);12     $show  = ‘‘;13     while ($crc32 > 0) {14         $s = $crc32 % 62;15         if ($s > 35) {16             $s = chr($s + 61);17         } elseif ($s > 9 && $s <= 35) {18             $s = chr($s + 55);19         }20         $show .= $s;21         $x = floor($crc32 / 62);22     }23     return $show;24 }25 26 echo shorturl2(‘http://www.google.com/‘);27 //4whP5428 29 //產生短網址方法230 function shortUrl2($input)31 {32     $base32 = array(33         ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘,34         ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘,35         ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘,36         ‘y‘, ‘z‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘37     );38 39     $hex       = md5($input);40     $hexLen    = strlen($hex);41     $subHexLen = $hexLen / 8;42     $output    = array();43     for ($i = 0; $i < $subHexLen; $i++) {44         // 把加密字元按照8位一組16進位與0x3FFFFFFF(30位1)進行位與運算45         $subHex = substr($hex, $i * 8, 8);46         $int    = 0x3FFFFFFF & (1 * (‘0x‘ . $subHex));47         $out    = ‘‘;48         for ($j = 0; $j < 6; $j++) {49             // 把得到的值與0x0000001F進行位與運算,取得字元數組chars索引50             $val = 0x0000001F & $int;51             $out .= $base32[$val];52             $int = $int >> 5;53         }54         $output[] = $out;55     }56     return $output;57 }58 59 $input = ‘http://www.google.com/‘;60 61 $output = shorturl($input);62 var_dump($output);

 

<?php


//產生短網址方法1
function shortUrl1($url)
{
if (empty($url)) {
return FALSE;
}
$url = crc32($url);
$crc32 = sprintf("%u", $url);
$show = ‘‘;
while ($crc32 > 0) {
$s = $crc32 % 62;
if ($s > 35) {
$s = chr($s + 61);
} elseif ($s > 9 && $s <= 35) {
$s = chr($s + 55);
}
$show .= $s;
$x = floor($crc32 / 62);
}
return $show;
}

echo shorturl2(‘http://www.google.com/‘);
//4whP54

//產生短網址方法2
function shortUrl2($input)
{
$base32 = array(
‘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‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘
);

$hex = md5($input);
$hexLen = strlen($hex);
$subHexLen = $hexLen / 8;
$output = array();
for ($i = 0; $i < $subHexLen; $i++) {
// 把加密字元按照8位一組16進位與0x3FFFFFFF(30位1)進行位與運算
$subHex = substr($hex, $i * 8, 8);
$int = 0x3FFFFFFF & (1 * (‘0x‘ . $subHex));
$out = ‘‘;
for ($j = 0; $j < 6; $j++) {
// 把得到的值與0x0000001F進行位與運算,取得字元數組chars索引
$val = 0x0000001F & $int;
$out .= $base32[$val];
$int = $int >> 5;
}
$output[] = $out;
}
return $output;
}

$input = ‘http://www.google.com/‘;

$output = shorturl($input);
var_dump($output);

 

php 兩種短網址產生方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.