php 生成短网址原理及代码_php技巧

WBOY
Release: 2016-05-17 08:50:40
Original
1085 people have browsed it

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。

代码如下:

复制代码 代码如下:

/** 生成短网址
* @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;

}
Related labels:
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 Tutorials
More>
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!