PHP - Figured out the bug (probably / partially) but still dont know how to fix it - How to express alpha numeric range

StackOverflow https://stackoverflow.com/questions/4841392

  •  27-10-2019
  •  | 
  •  

Question

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'));

Was it helpful?

Solution

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.

OTHER TIPS

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.

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top