PHP生成短網(wǎng)址的3種方法代碼實例
短網(wǎng)址服務(wù),可能很多朋友都已經(jīng)不再陌生,現(xiàn)在大部分微博、手機郵件提醒等地方已經(jīng)有很多應(yīng)用模式了,并占據(jù)了一定的市場。估計很多朋友現(xiàn)在也正在使用。 看過新浪的短連接服務(wù),發(fā)現(xiàn)后面主要有6個字符串組成。
太多算法的東西,也沒必要去探討太多,最主要的還是實現(xiàn),下面是三種方法的代碼:
<?php //純隨機生成方法 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); // 枚舉生成方法 function shorturl($input) { $base32 = 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 .= $base32[$val]; $int = $int >> 5; } $output[] = $out; } return $output; } $a=shorturl("http://www.dbjr.com.cn"); print_r($a); //62 位生成方法 function base62($x) { $show= ''; while($x> 0) { $s= $x% 62; if($s> 35) { $s= chr($s+61); } elseif($s> 9 && $s<=35) { $s= chr($s+ 55); } $show.= $s; $x= floor($x/62); } return $show; } function urlShort($url) { $url= crc32($url); $result= sprintf("%u", $url); return base62($result); } echo urlShort("http://www.dbjr.com.cn/"); ?>
- 一個php短網(wǎng)址的生成代碼(仿微博短網(wǎng)址)
- php簡單實現(xiàn)短網(wǎng)址(短鏈)還原的方法(測試可用)
- PHP生成短網(wǎng)址方法匯總
- PHP將URL轉(zhuǎn)換成短網(wǎng)址的算法分享
- PHP長網(wǎng)址與短網(wǎng)址的實現(xiàn)方法
- php生成短網(wǎng)址示例
- PHP通過調(diào)用新浪API生成t.cn格式短網(wǎng)址鏈接的方法詳解
- PHP生成短網(wǎng)址的思路以及實現(xiàn)方法的詳解
- PHP利用DWZ.CN服務(wù)生成短網(wǎng)址
- php 短鏈接算法收集與分析
- php調(diào)用新浪短鏈接API的方法
- php生成短網(wǎng)址/短鏈接原理和用法實例分析
相關(guān)文章

PHP連接局域網(wǎng)MYSQL數(shù)據(jù)庫的簡單實例

CodeIgniter配置之database.php用法實例分析

laravel 實現(xiàn)向公共模板中傳值 (view composer)

PHPMailer使用教程(PHPMailer發(fā)送郵件實例分析)