javascript到php转换.chars.substring

时间:2013-11-28 18:40:01

标签: javascript php

我正在尝试将下面的javascript代码转换为php

function randomString(len) {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmn opqrstuvwxyz";
    var string_length = len;
    var randomstring = '';
    for (var i = 0; i < string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum, rnum + 1);
    }

    return randomstring;
}

我的PHP代码到目前为止.. 编辑:下面的工作代码

     function randomString($len) {
        $chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
        $string_length = strlen($chars);
        $randomstring = '';
        for ($i = 0; $i< $len; $i++) {
         $rnum = rand(0,$string_length);
         $randomstring.= $chars[$rnum];
        }

        return $randomstring;
    }

echo randomString(16);

chars.substring(rnum,rnum + 1)是什么意思......我怎样才能在php中实现...我能理解它需要$ chars的特定字符...但是rnum + 1是什么为了?

1 个答案:

答案 0 :(得分:0)

你这样做:

$ randomstring。= substr($ chars,$ rnum,$ rnum + 1);

阅读docs