php 產生短網址原理及代碼_PHP教程

來源:互聯網
上載者:User
php 產生短網址

原理:

1.將原網址做crc32校正,得到校正碼。

2.使用sprintf('%u') 將校正碼轉為無符號數字。

3.對無符號數字進行求餘62操作(大小寫字母+數字等於62位),得到餘數後映射到62個字元中,將映射後的字元儲存。(例如餘數是10,則映射的字元是A,0-9對應0-9,10-35對應A-Z,35-62對應a-z)

4.迴圈操作,直到數值為0。

5.將所有映射後的字元拼接,就是短網址後的code。

代碼如下:
複製代碼 代碼如下:
/** 產生短網址
* @param String $url 原網址
* @return String
*/
function dwz($url){

$code = sprintf('%u', crc32($url));

$surl = '';

while($code){
$mod = $code % 62;
if($mod>9 && $mod<=35){
$mod = chr($mod + 55);
}elseif($mod>35){
$mod = chr($mod + 61);
}
$surl .= $mod;
$code = floor($code/62);
}

return $surl;

}

http://www.bkjia.com/PHPjc/726024.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/726024.htmlTechArticlephp 產生短網址 原理: 1.將原網址做crc32校正,得到校正碼。 2.使用sprintf('%u') 將校正碼轉為無符號數字。 3.對無符號數字進行求餘62操作(大...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.