PHP - 找出了错误(可能/部分),但仍然不知道如何修复它 - 如何表达字母数字范围

发布于 2024-10-14 16:40:07 字数 872 浏览 4 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她如夕阳 2024-10-21 16:40:07
rand(0,68)

我只计算了该数组中的 62 个元素:

A-Z (26)
a-z (26)
0-9 (10)
    ----
     62

也许我的数学有问题? (即使你的 ideone 显示索引 0-61 ;-p)

我的猜测是你得到的索引超出了范围数组,它不会添加它们(也不应该)。您可能还启用了错误抑制功能,因此它不会被注意到。

rand(0,68)

I only count 62 element in that array:

A-Z (26)
a-z (26)
0-9 (10)
    ----
     62

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.

感性不性感 2024-10-21 16:40:07

我的建议如下:

$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$pool = str_split($chars, 1);
$max = count($pool) - 1;

$random = '';
for ($i = 1; $i <= 5; $i ++) {
    $random .= $pool[mt_rand(0, $max)];
}

echo $random;

它可以更轻松地查看允许的字符是什么,并且不需要一堆 range() 和合并。

这实际上几乎直接来自 Kohana 核心: https://github.com/kohana/core/blob/3.0/develop/classes/kohana/text.php#L140 它们提供了各种字符组,允许不同的随机集,包括不同的字符集对于密码生成非常有用。

My recommendation would be to the following:

$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$pool = str_split($chars, 1);
$max = count($pool) - 1;

$random = '';
for ($i = 1; $i <= 5; $i ++) {
    $random .= $pool[mt_rand(0, $max)];
}

echo $random;

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.

狼性发作 2024-10-21 16:40:07

问题出在 rand(0,68) 中,因为您的字母表有 62 个字符,而不是 68 个。因此,当 rand 返回整数 63 到 67 时,一个空字符串会附加到 < code>$var,从而产生一个较短的 5 个字符的字符串(如果你“足够不幸”的话甚至更少)。

不管怎样,我认为如果你只使用 array_rand 会更清楚:

$char = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
for ($i = 0; $i < 6; $i++) {
    $var .= $char[array_rand($char)];
}
echo $var;

The problem is in rand(0,68) because your alphabet has 62 characters, not 68. So when rand 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:

$char = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
for ($i = 0; $i < 6; $i++) {
    $var .= $char[array_rand($char)];
}
echo $var;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文