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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀