短链接的自我写法,重复率有多高还没测试 发布日期:2017-01-05 09:59:06     博主推荐★

之前面试的时候,有面试官和我说md5做不了短链接,我也不知道他是怎么做,我感觉这样的做法应该没问题啊!


<?php
   $or_url    = $_GET['or_url'];
   $md5_url   = md5($or_url);
   
   $salt      = '0826';//这里不高于aaaa就不会出现61以上的数字,所以来说,后面的特别符号是不会出现的!
   $md5_url   = addSalt($md5_url,$salt);
   
   $short_url = '';
   
   for($i=0;$i<6;$i++){
   $current_str = substr($md5_url,$i*6,6);
   //处理当前串的值
   $short_url .= ZChar($current_str);
   }
   
   echo $short_url;
   
   function addSalt($md5_url,$salt){
   if(strlen($md5_url) != 32){
   die('addSalt:{$md5_url} error');
   }
   $len  = strlen($salt);
   for($i=0;$i<$len;$i++){
   $position = hexdec($md5_url[$i]);
   $weight   = ceil($position/8);
   $position = $position * $weight;
   $pre      = substr($md5_url,0,$position);
   $next     = substr($md5_url,$position);
   $md5_url  = $pre.$salt[$i].$next;
   }
   return $md5_url;
   }
   
   function dealStr($str){
   if(strlen($str) != 6){
   die("dealStr:{$str}error");
   }
   $count = '';
   for($i=0;$i<6;$i++){
   //加权,避免不同序列的和一致
   $number = hexdec($str[$i])*($i+1)%12;
   $count += $number;
   }
   
   return $count % 66;
   }
   
   function ZChar($current_str){
   $Vchar = dealStr($current_str);
   return char($Vchar);
   }
   
   
   function char($char_point){
   
    $char_array = ["0","1","2","3","4","5",   
                "6","7","8","9","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","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",
"$","@",'+',"="];
    $char_point = intval($char_point);
if($char_point < 0 || $char_point > 65){
die('char:'.$char_point.' error');
}
    
return $char_array[$char_point];
   }
?>


博文地址:https://blog.ahamu.cn/blog/detail.html?id=88
   
推荐文章
  • 1
    sysbench
    2020/07/08
  • 2
    phper转java记录篇-spring boot
    2020/06/10
  • 3
    thinkphp5.0使用路由之后,post请求的
    2020/05/19
  • 4
    springboot单元测试aop失效
    2020/05/15
  • 5
    脑海中的JVM
    2020/05/12
  • 6
    IDEA搜索插件时显示search results
    2020/05/12
  • 7
    spring boot 配置加载源码查找
    2020/04/20
  • 8
    通过javap命令分析java汇编指令
    2020/04/16
  • 9
    IDEA小知识:查看JVM内存使用情况的步骤
    2020/04/16
  • 10
    springboot-加载自定义的properti
    2020/04/14
  • 11
    Jenkins执行shell脚本无法启动子进程解决
    2020/04/03
  • 12
    mac idea激活找专业的
    2020/04/02
  • 13
    Jenkins + DockerSwarm 实现弹
    2020/03/31
  • 14
    mac swarm学习过程
    2020/03/31
  • 15
    spring cloud
    2020/03/18
  • 16
    JAVA开发中遇到的问题记录002
    2020/03/12
  • 17
    JAVA开发中遇到的问题记录001
    2020/03/07
  • 18
    php -i查看信息
    2020/02/23
  • 19
    phpStorm中使用xdebug工具调试dock
    2019/12/09
  • 20
    讲的比较好的B+树执行原理的文章
    2019/12/09
最喜标签
NYOJ 面试 AJAX ping CentOS 灰度算法 YII2