PHP - 找出了错误(可能/部分),但仍然不知道如何修复它 - 如何表达字母数字范围
我有这个 PHP 代码:
$char = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
$i = 0;
while ($i <= 5) {
$var .= $char[rand(0,68)];
$i++;
}
echo $var;
乍一看,它应该生成一个 6 个字符的字符串(例如:eVx97j)。
大多数时候我都会这样做,但在某些情况下它会回显 5 和 4 个字符串。
我几乎可以肯定这是因为这个:(
可能与问题无关 )
[50]=>
string(1) "Y"
[51]=>
string(1) "Z"
[52]=>
int(0)
[53]=>
int(1)
[54]=>
int(2)
看到区别了吗?
让我困惑的是,代码确实生成带有数字的字符串。
那么你能发现这个错误吗?
这是 PHP 中表达 az + AZ + 0-9 字符的正确方法吗?
提前致谢!!
请询问任何需要澄清的问题!
顺便说一句:我也尝试过: $char = array_merge(range('a', 'z'), range( 'A', 'Z'), 范围('0', '9'));
I have this PHP code:
$char = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
$i = 0;
while ($i <= 5) {
$var .= $char[rand(0,68)];
$i++;
}
echo $var;
At a glance it should generate a 6 character sting (like: eVx97j).
I does most of the times, but in some cases it echos 5 and 4 character strings.
I almost sure it is because of this:
(COULD HAVE NOTHING TO DO WITH THE PROBLEM)
[50]=>
string(1) "Y"
[51]=>
string(1) "Z"
[52]=>
int(0)
[53]=>
int(1)
[54]=>
int(2)
See the difference?
What puzzles me is that the code does generate string with numbers.
So can you spot the bug?
Is this the correct way in PHP to express a-z + A-Z + 0-9 characters?
Thanks in advance!!
Please ask for any clarification needed!
BTW: I've also tried: $char = array_merge(range('a', 'z'), range('A', 'Z'), range('0',' 9'));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只计算了该数组中的 62 个元素:
也许我的数学有问题? (即使你的 ideone 显示索引 0-61 ;-p)
我的猜测是你得到的索引超出了范围数组,它不会添加它们(也不应该)。您可能还启用了错误抑制功能,因此它不会被注意到。
I only count 62 element in that array:
Maybe my math's off? (Even your ideone shows indexes 0-61 ;-p)
My guess is you're getting indexes outside the bounds of the array and it's not going to add them (nor should it). You also probably have error suppressing on so it's getting by unnoticed.
我的建议如下:
它可以更轻松地查看允许的字符是什么,并且不需要一堆 range() 和合并。
这实际上几乎直接来自 Kohana 核心: https://github.com/kohana/core/blob/3.0/develop/classes/kohana/text.php#L140 它们提供了各种字符组,允许不同的随机集,包括不同的字符集对于密码生成非常有用。
My recommendation would be to the following:
It makes it much easier to see what the characters that will be allowed are and also doesn't require a bunch of range() and merges.
This is actually pretty much straight out of the Kohana Core: https://github.com/kohana/core/blob/3.0/develop/classes/kohana/text.php#L140 They provide a variety of groups of characters allowing for different random sets, including distinct which is very useful for password generation.
问题出在
rand(0,68)
中,因为您的字母表有 62 个字符,而不是 68 个。因此,当rand
返回整数 63 到 67 时,一个空字符串会附加到 < code>$var,从而产生一个较短的 5 个字符的字符串(如果你“足够不幸”的话甚至更少)。不管怎样,我认为如果你只使用
array_rand
会更清楚:The problem is in
rand(0,68)
because your alphabet has 62 characters, not 68. So whenrand
returns integers 63 to 67, an empty string is appended to$var
, thus resulting in a shorter string of 5 characters (or even less if you're "unlucky enough").Anyway, I think it's clearer if you just use
array_rand
: