我在PHP中有这个:
$_chars = "0123456789ZXCVBNMASDFGHJKLQWERTYUIOP";
for($l = 0; $l<4; $l++){
$temp = str_shuffle($_chars);
$_charcode .= $temp;
}
我希望它只生成4个字符。目前它正在生成6.我已经尝试编辑$ l但它没有改变任何东西。
答案 0 :(得分:2)
文档(http://php.net/str_shuffle)声明:
str_shuffle()随机播放一个字符串。创造了所有可能的一种排列。
它实际上应该生成4 * strlen($_chars)
个字符......
我假设你想要:
$_charcode .= $temp[0]; // only one character
答案 1 :(得分:1)
来自文档:
str_shuffle()
随机播放一个字符串。创造了所有可能的一种排列。
您只想从随机字符串中检索一个字符:
$_charcode .= $temp[0];
因此,代码应如下所示:
$_chars = "0123456789ZXCVBNMASDFGHJKLQWERTYUIOP";
$_charcode = ''; // initialize the variable with an empty string
for($l = 0; $l<4; $l++){
$temp = str_shuffle($_chars);
$_charcode .= $temp[0];
}
echo $_charcode;
输出(示例):
8VG6