doucan8521 2011-12-02 20:23
浏览 21
已采纳

如何在PHP中增加一个字母数字字符串?

There's a string, containing characters [a-zA-Z0-9]. This should be 26 * 2 + 10 = 62 possibilities in one character and 62^2 in two. What is the preferred way to increment a value of such a string, so that 'aA', becomes 'aB', etc.? Is there any built-in stuff in PHP, that can help?

I know you can increment a string, but that's only lowercase letters. Essentially, the result should go from 'a' to 'aa' in 61 increments.

  • 写回答

6条回答 默认 最新

  • dpmwy80068 2011-12-02 20:46
    关注

    The @simshaun dont work for me. I check the documentation and I found base_convert that can work for you (on base 35) and a comment of "francesco[at]paladinux.net" with a function to work on base65.

    so the solution can be:

    convert to b10 -> increment +1 -> convert base 65

    EDIT 1

    talking about convert I think to base64 coding so I wrote this 2 funcs using base64 encode/decode numbers. Unfortunately the charset used is a bit larger: [a-zA-Z0-9/+=], but using an internal function more efficent.

    The Zero 0 is "AA=="

    function nencode($in){
            $res ="";
            while(1){
                    $b = $in & 0xff;
                    $res = chr($b) . $res;
    
                    if($in > 0xff){
                            $in = $in >> 8;
                    } else {
                            break;
                    }
            }
            return base64_encode($res);
    }
    
    function ndecode($in){
    
            $rv=0;
            $res =base64_decode($in);
            for($t = 0 ; $t < strlen($res) ; $t++){
                    if($t>0){
                            $rv = $rv << 8;
                    }
                    $c = ord($res{$t});
                    $rv |= $c;
            }
            return $rv;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答