短いウェブサイト

4508 ワード

考え方一:
<?php
function shorturl($url='') {
$base = 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" ,
              "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");

$hex = md5($url);
$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 = 0x0000003D & $int;//  6         26+26+10   
        $out .= $base[$val];
        $int = $int >> 5;// $int30   ,    5 ,      6  
    }
    $output[] = $out;
    }
    return $output;
}

$urls = shorturl('http://www.php100.com');
echo "<pre>";var_dump($urls);exit();
?>

 
 
考え方2:
もう1つは,データベースに格納し,増分IDから取り,10=』62進数変換を行い,結果として短いURLとなり,アクセス時にデータベースから真のアドレスを取得する.