Home> php教程> php手册> body text

php 生成短网址的一例代码

WBOY
Release: 2016-06-07 11:34:40
Original
1121 people have browsed it

php 生成短网址的一例代码
php生成短网址的原理:
1.将原网址做crc32校验,得到校验码。
2.使用sprintf('%u') 将校验码转为无符号数字。
3.对无符号数字进行求余62操作(大小写字母+数字等于62位),得到余数后映射到62个字符中,将映射后的字符保存。(例如余数是10,则映射的字符是A,0-9对应0-9,10-35对应A-Z,35-62对应a-z)
4.循环操作,直到数值为0。
5.将所有映射后的字符拼接,就是短网址后的code。

例1,php短网址实现代码。


代码示例:
/** 生成短网址
* @param String $url 原网址
* @return String
*/
function dwz($url){

$code = sprintf('%u', crc32($url));

$surl = '';

while($code){
$mod = $code % 62;
if($mod>9 && $mod $mod = chr($mod + 55);
}elseif($mod>35){
$mod = chr($mod + 61);
}
$surl .= $mod;
$code = floor($code/62);
}

return $surl;

}
生成短网址示例:


代码示例:
echo dwz('http://www.heimaolianmeng.com'); //lVRl4
?>

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!