PHP 實現短url代碼

<?phpphp


function shortUrl($url)url

{spa

$md5_str = md5($url);md5

$md5_arr = str_split($md5_str,16);it

$ret = array();io

$seed_arr = array_merge(range('a','z'),range(0,9),range('A','Z'));function

$seed_len = count($seed_arr);foreach


foreach($md5_arr as $str){bind

 $bin = strTobin($str);co

 $bin_len = strlen($bin);

 $avg_len = ceil($bin_len/6);

 $bin_arr = str_split($bin,$avg_len);

 $tmp_str = '';

 foreach($bin_arr as $bin){

 $index = bindec($bin)%$seed_len;

 $tmp_str .= $seed_arr[$index];

 }


 $ret[] = $tmp_str;


}


print_r($ret);


 }




function strTobin($str)

{

$bin = '';

for($i=0,$len=strlen($str);$i<$len;$i++){

     $bin .= str_pad(base_convert($str{$i}, 16, 2),4,'0', STR_PAD_LEFT);

     }


     return $bin;

}

$url = 'http://wenku.baidu.com/link?url=rvpWe98e3D2-W70FO6G_LB2WyElgeTNsGwKWSakl5MJu-EaVPd5r39l7-5ngTNdxi2Yp1FX6A4-neb-MNKCApJKwl9tVYkCN4lMpxuHBvdq';

shortUrl($url);

相關文章
相關標籤/搜索