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条)

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题