dongzuoyue6556 2013-11-26 17:12
浏览 52
已采纳

PHP URL Shortener错误

I have this PHP code which is supposed to increase a URL shortener mask on each new entry. My problem is that it dosen't append a new char when it hits the last one (z). (I know incrementing is a safety issue since you can guess earlier entries, but this is not a problem in this instance)

If i add 00, it can figure out 01 and so on... but is there a simple fix to why it won't do it on its own?

(The param is the last entry)

<?php

class shortener
{

    public function ShortURL($str = null)
    {
        if (!is_null($str))
        {
            for($i = (strlen($str) - 1);$i >= 0;$i--)
            {
                if($str[$i] != 'Z')
                {
                    $str[$i] = $this->_increase($str[$i]);
                    #var_dump($str[$i]);
                    break;
                }
                else
                {
                    $str[$i] = '0';
                    if($i == 0)
                    {
                        $str = '0'.$str;
                    }
                }
            }
            return $str;
        }
        else {
            return '0';
        }
    }

    private function _increase($letter)
    {
        //Lowercase: 97 - 122
        //Uppercase: 65 - 90
        //  0 - 9  : 48 - 57
        $ord = ord($letter);
        if($ord == 122)
        {
            $ord = 65;
        }
        elseif ($ord == 57)
        {
            $ord = 97;
        }
        else
        {
            $ord++;
        }
        return chr($ord);
    }
}

?>
  • 写回答

1条回答 默认 最新

  • dsfdsfds521521 2013-11-26 17:46
    关注

    Effectively, all you are doing is encoding a number into Base62. So if we take the string, decode it into base 10, increment it, and reencode it into Base62, it will be much easier to know what we are doing, and the length of the string will take care of itself.

    class shortener
    {
      public function ShortURL($str = null)
      {
        if ($str==null) return 0;
        $int_val = $this->toBase10($str);
        $int_val++;
        return $this->toBase62($int_val);
      }
    
      public function toBase62($num, $b=62) {
        $base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $r = $num  % $b ;
        $res = $base[$r];
        $q = floor($num/$b);
        while ($q) {
          $r = $q % $b;
          $q =floor($q/$b);
          $res = $base[$r].$res;
        }
        return $res;
      }
    
      function toBase10( $num, $b=62) {
        $base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $limit = strlen($num);
        $res=strpos($base,$num[0]);
        for($i=1;$i<$limit;$i++) {
          $res = $b * $res + strpos($base,$num[$i]);
        }
        return $res;
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示