使用以下PHP代码可以生成唯一的6位的短网址。

代码如下:

 <?php

 //生成短网址方法1
function shortUrl1($url)
{
if (empty($url)) {
return FALSE;
}
$url = crc32($url);
$crc32 = sprintf("%u", $url);
$show = '';
while ($crc32 > 0) {
$s = $crc32 % 62;
if ($s > 35) {
$s = chr($s + 61);
} elseif ($s > 9 && $s <= 35) {
$s = chr($s + 55);
}
$show .= $s;
$x = floor($crc32 / 62);
}
return $show;
} echo shorturl2('http://www.google.com/');
//4whP54 //生成短网址方法2
function shortUrl2($input)
{
$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($input);
$hexLen = strlen($hex);
$subHexLen = $hexLen / 8;
$output = array();
for ($i = 0; $i < $subHexLen; $i++) {
// 把加密字符按照8位一组16进制与0x3FFFFFFF(30位1)进行位与运算
$subHex = substr($hex, $i * 8, 8);
$int = 0x3FFFFFFF & (1 * ('0x' . $subHex));
$out = '';
for ($j = 0; $j < 6; $j++) {
// 把得到的值与0x0000001F进行位与运算,取得字符数组chars索引
$val = 0x0000001F & $int;
$out .= $base32[$val];
$int = $int >> 5;
}
$output[] = $out;
}
return $output;
} $input = 'http://www.google.com/'; $output = shorturl($input);
var_dump($output);

<?php

//生成短网址方法1
function shortUrl1($url)
{
if (empty($url)) {
return FALSE;
}
$url = crc32($url);
$crc32 = sprintf("%u", $url);
$show = '';
while ($crc32 > 0) {
$s = $crc32 % 62;
if ($s > 35) {
$s = chr($s + 61);
} elseif ($s > 9 && $s <= 35) {
$s = chr($s + 55);
}
$show .= $s;
$x = floor($crc32 / 62);
}
return $show;
}

echo shorturl2('http://www.google.com/');
//4whP54

//生成短网址方法2
function shortUrl2($input)
{
$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($input);
$hexLen = strlen($hex);
$subHexLen = $hexLen / 8;
$output = array();
for ($i = 0; $i < $subHexLen; $i++) {
// 把加密字符按照8位一组16进制与0x3FFFFFFF(30位1)进行位与运算
$subHex = substr($hex, $i * 8, 8);
$int = 0x3FFFFFFF & (1 * ('0x' . $subHex));
$out = '';
for ($j = 0; $j < 6; $j++) {
// 把得到的值与0x0000001F进行位与运算,取得字符数组chars索引
$val = 0x0000001F & $int;
$out .= $base32[$val];
$int = $int >> 5;
}
$output[] = $out;
}
return $output;
}

$input = 'http://www.google.com/';

$output = shorturl($input);
var_dump($output);

php 两种短网址生成方法的更多相关文章

  1. 如何做系列(4)-微博URL短网址生成算法原理(java版、php版实现实例)

    短网址(Short URL),顾名思义就是在形式上比较短的网址.通常用的是asp或者php转向,在Web 2.0的今天,不得不说,这是一个潮流.目前已经有许多类似服务,借助短网址您可以用简短的网址替代 ...

  2. SSH简介及两种远程登录的方法

    出处 https://blog.csdn.net/li528405176/article/details/82810342 目录 SSH的安全机制 SSH的安装 启动服务器的SSH服务 SSH两种级别 ...

  3. C#两种创建快捷方式的方法

    C#两种创建快捷方式的方法http://www.cnblogs.com/linmilove/archive/2009/06/10/1500989.html

  4. HTTP/HTTPS GET&POST两种方式的实现方法

    关于GET及POST方式的区别请参照前面文章:http://www.cnblogs.com/hunterCecil/p/5698604.html http://www.cnblogs.com/hunt ...

  5. iOS - UITableView中有两种重用Cell的方法

    UITableView中有两种重用Cell的方法: - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; - (id)dequ ...

  6. 两种ps切图方法(图层/切片)

    两种Ps切图方法 一.      基础操作: a)    Ctrl++ 放大图片,ctrl - -缩小图片 b)    按住空格键space+,点击鼠标左键,拖动图片. c)    修改单位,点击编辑 ...

  7. Eclipse中SVN的安装步骤(两种)和使用方法

    Eclipse中SVN的安装步骤(两种)和使用方法 一.给Eclipse安装SVN,最常见的有两种方式:手动方式和使用安装向导方式.具体步骤如下: 方式一:手动安装 1.下载最新的Eclipse,我的 ...

  8. TextView两种显示link的方法

    TextView两种显示link的方法 一.简介 也是TextView显示文本控件两种方法 也是显示丰富的文本 二.方法 TextView两种显示link的方法  1)通过TextView里面的类ht ...

  9. php短网址生成算法

    <?php //短网址生成算法 class ShortUrl { //字符表 public static $charset = "0123456789ABCDEFGHIJKLMNOPQ ...

随机推荐

  1. 使用opatch工具 打补丁Patch 21352635 -(Database Patch Set Update 11.2.0.4.8)

    Patch 21352635 - Database Patch Set Update 11.2.0.4.8 一.OPatch工具检查及升级 OPatch工具包,在安装目录$ORACLE_HOME下,P ...

  2. SQL 函数 DateDiff()

    DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]]) 描述 返回两个日期之间的时间间隔. 语法 DateDiff( ...

  3. 使用Bulk Binding批量绑定的模式高效处理ORACLE大量数据

           用批量绑定(bulk binding)的方式.当循环执行一个绑定变量的sql语句时候,在PL/SQL 和SQL引擎(engines)中,会发生大量的上下文切换(context switc ...

  4. ES curl bulk 导入数据

    比如我们现在有这样一个文件,data.json: { " } } { "field1" : "value1" } 它的第一行定义了_index,_ty ...

  5. 管道的创建与读写pipe

    1.管道的创建 #include <unistd.h> int pipe(int pipefd[2]); linux下创建管道可以通过函数pipe来完成.该函数如果调用成功,数组中将包含两 ...

  6. BZOJ3544 [ONTAK2010]Creative Accounting

    看不懂题,就不能写的稍微像人话点吗我去... 题目就是要找一段区间使得Σai mod m的值最大. 于是嘛...前缀和一下再贪心就好了. 先求出前i个数的前缀和s,然后用s更新解. 还有可能就是前面的 ...

  7. learning shell script prompt to run with superuser privileges (4)

    Shell script prompt to run with superuser privileges [Purpose]        Check whether have root privil ...

  8. json添加数据

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. bzoj1599

    题解: 暴力枚举每一种可能性 代码: #include<bits/stdc++.h> using namespace std; int s1,s2,s3,ans,num; int main ...

  10. DevExpress v17.2新版亮点—DevExtreme篇(二)

    用户界面套包DevExpress DevExtreme v17.2终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExtreme v17.2 的New Color Sche ...