欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP生成短網(wǎng)址的3種方法代碼實例

 更新時間:2014年07月08日 10:21:35   投稿:junjie  
這篇文章主要介紹了PHP生成短網(wǎng)址的3種方法代碼實例,分別為純隨機生成法、枚舉生成法、62位生成法,需要的朋友可以參考下

短網(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/"); 
 
?>

相關(guān)文章

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

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

    這篇文章介紹了PHP連接局域網(wǎng)MYSQL數(shù)據(jù)庫的簡單實例,有需要的朋友可以參考一下
    2013-08-08
  • Yii2中Restful API原理實例分析

    Yii2中Restful API原理實例分析

    這篇文章主要介紹了Yii2中Restful API原理,基于rest部分源碼分析了Restful的原理、使用方法與相關(guān)注意事項,需要的朋友可以參考下
    2016-07-07
  • CodeIgniter配置之database.php用法實例分析

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

    這篇文章主要介紹了CodeIgniter配置之database.php用法,結(jié)合實例形式較為詳細的分析總結(jié)了CodeIgniter常用的數(shù)據(jù)庫連接方式,需要的朋友可以參考下
    2016-01-01
  • laravel 實現(xiàn)向公共模板中傳值 (view composer)

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

    今天小編就為大家分享一篇laravel 實現(xiàn)向公共模板中傳值 (view composer),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • Zend Framework+smarty用法實例詳解

    Zend Framework+smarty用法實例詳解

    這篇文章主要介紹了Zend Framework+smarty用法,結(jié)合實例形式詳細分析了Zend Framework框架整合Smarty模板的具體步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2016-03-03
  • PHPMailer使用教程(PHPMailer發(fā)送郵件實例分析)

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

    php雖然提供了mail()函數(shù),但并不好用,而PHPMailer是一個不錯的郵件發(fā)送工具,接下來將詳細介紹,需要了解的朋友可以參考下
    2012-12-12
  • 最新評論