php實(shí)現(xiàn)的短網(wǎng)址算法分享
每個(gè)網(wǎng)址用6個(gè)字符代替,(6^32) 最多可以擁有1,073,741,824個(gè)短網(wǎng)址。
當(dāng)然,你還可以記錄更詳細(xì)的信息,如訪問(wèn)記錄,創(chuàng)建時(shí)間等。
如果真不夠用了,還可以刪掉很久不用的。
function shorturl($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++) {
$subHex = substr ($hex, $i * 8, 8);
$int = 0x3FFFFFFF & (1 * ('0x'.$subHex));
$out = '';
for ($j = 0; $j < 6; $j++) {
$val = 0x0000001F & $int;
$out .= $base32[$val];
$int = $int >> 5;
}
$output[] = $out;
}
return $output;
}
測(cè)試代碼:
$input = 'http://www.dbjr.com.cn/1';
$output = shorturl($input);
echo "Input : $input\n";
echo "Output : {$output[0]}\n";
echo " {$output[1]}\n";
echo " {$output[2]}\n";
echo " {$output[3]}\n";
echo "\n";
$input = 'http://www.dbjr.com.cn/2';
$output = shorturl($input);
echo "Input : $input\n";
echo "Output : {$output[0]}\n";
echo " {$output[1]}\n";
echo " {$output[2]}\n";
echo " {$output[3]}\n";
echo "\n";
輸出:
Input : http://www.dbjr.com.cn/1
Output : h0xg4r
bdr3tw
osk2d3
4azfqa
Input : http://www.dbjr.com.cn/2
Output : tm5kxb
ceoj2s
yw3dvl
nrmrxl
- PHP長(zhǎng)網(wǎng)址與短網(wǎng)址的實(shí)現(xiàn)方法
- PHP將URL轉(zhuǎn)換成短網(wǎng)址的算法分享
- PHP生成短網(wǎng)址方法匯總
- php簡(jiǎn)單實(shí)現(xiàn)短網(wǎng)址(短鏈)還原的方法(測(cè)試可用)
- php短網(wǎng)址和數(shù)字之間相互轉(zhuǎn)換的方法
- PHP生成短網(wǎng)址的3種方法代碼實(shí)例
- 一個(gè)php短網(wǎng)址的生成代碼(仿微博短網(wǎng)址)
- php生成短網(wǎng)址示例
- php 生成短網(wǎng)址原理及代碼
- PHP通過(guò)調(diào)用新浪API生成t.cn格式短網(wǎng)址鏈接的方法詳解
相關(guān)文章
淺談php數(shù)組array_change_key_case() 函數(shù)和array_chunk()函數(shù)
下面小編就為大家?guī)?lái)一篇淺談php數(shù)組array_change_key_case() 函數(shù)和array_chunk()函數(shù)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10smarty簡(jiǎn)單分頁(yè)的實(shí)現(xiàn)方法
這篇文章主要介紹了smarty簡(jiǎn)單分頁(yè)的實(shí)現(xiàn)方法,以一個(gè)具體的實(shí)例講述了smarty分頁(yè)的完整實(shí)現(xiàn)過(guò)程,需要的朋友可以參考下2014-10-10針對(duì)thinkPHP5框架存儲(chǔ)過(guò)程bug重寫(xiě)的存儲(chǔ)過(guò)程擴(kuò)展類完整實(shí)例
這篇文章主要介紹了針對(duì)thinkPHP5框架存儲(chǔ)過(guò)程bug重寫(xiě)的存儲(chǔ)過(guò)程擴(kuò)展類,結(jié)合完整實(shí)例形式給出了修復(fù)thinkPHP5存儲(chǔ)過(guò)程原有bug的擴(kuò)展類定義與使用方法,需要的朋友可以參考下2018-06-06PHP連接MYSQL數(shù)據(jù)庫(kù)實(shí)例代碼
現(xiàn)在做的項(xiàng)目需要php連接mysql數(shù)據(jù)庫(kù),雖然之前學(xué)過(guò),但是現(xiàn)在基本上都給忘了,之后通過(guò)查找相關(guān)資料找到了解決方法,下面小編把具體方法分享在腳本之家平臺(tái)供大家學(xué)習(xí)2016-01-01Laravel-添加后臺(tái)模板AdminLte的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Laravel-添加后臺(tái)模板AdminLte的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10PHP給文字內(nèi)容中的關(guān)鍵字進(jìn)行套紅處理
本文介紹了一個(gè)PHP中的函數(shù),可以對(duì)內(nèi)容的關(guān)鍵字進(jìn)行套紅,高亮突出顯示關(guān)鍵字,分享給大家,希望對(duì)大家有所幫助。2016-04-04