使用以下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);

最新文章

  1. 6.openstack之mitaka搭建网络节点
  2. C# LINQ
  3. EBS 消息总线
  4. fastcgi配置
  5. jq 构造函数,然后再表单提交过程中对数据进行修改
  6. CISCO系列交换机如何清空配置
  7. [JAVA设计模式]第三部分:结构模式
  8. 【温故知新】c#异步编程模型(APM)--使用委托进行异步编程
  9. jQuery的datatable怎么才能给某一列添加超链接?
  10. 解决cookie无法删除的问题
  11. Intersecting Lines(数学)
  12. Twenty Newsgroups Classification任务之二seq2sparse
  13. 在iOS7中改动状态栏字体的颜色
  14. 汇总博客常见的api接口地址(windows live write)
  15. Centos 7 ip查看问题
  16. BC#65 T5 ZYB&#39;s Prime
  17. Django ORM中,如何使用Count来关联对象的子集数量
  18. JavaSpcript基础
  19. T-SQL 日期函数
  20. spring拦截器中使用spring的自动注入

热门文章

  1. Google浏览器设置变更默认搜索引擎为百度
  2. 根据服务端生成的WSDL文件创建客户端支持代码的三种方式
  3. OC Foundation框架—字符串操作方法及习题
  4. pymysql 单独获取表的栏位名称
  5. Kafka生产者APi
  6. js中定时器
  7. WiFi Pineapple的Karma攻击与原理探究
  8. UI基础:UIScrollView、UIPageControl
  9. JavaWeb中监听器+过滤器+拦截器区别、配置和实际应用
  10. SharePoint 2010: Change welcome page on PowerShell