<?php namespace Common\Common\ShortUrl; /** 長連接轉換短連接 */ class ShortUrl { #字符表 public static $charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; public static function shortUrl($url) { $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($url); $hexLen = strlen($hex); $subHexLen = $hexLen / 8; $short_url_list = 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; } $short_url_list[] = $out; } return $short_url_list; } }
參考:http://www.voidcn.com/article/p-ydjqllgt-ed.htmlphp
更多php技術交流,可加Q羣:884743303,裏面各路大神爲您保駕護航,html