1
<?php

function pass($level=2,$length=6) {

    $chars[1] = "023456789abcdefghijmnopqrstuvwxyz";
    $chars[2] = "23456789abcdefghijmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";

    $i = 0;
    $str = "";

    while ($i<=$length) {
        $str .= $chars[$level][mt_rand(0,strlen($chars[$level]))];
        $i++;
    }

    return $str;

}

echo pass(2, 7);

?>

When I call the function, I really can't set anything up. pass(2,7) is the same length as pass(1, 9). It's all level 2 and some length. What's wrong?

2

4 Answers 4

1

The other answers seem to ignore the fact that strings are treated as arrays of characters in php (see the section String access and modification by character).

Your script worked fine for me with two modifications:

First:

You are referencing a character randomly from 0 to the length of the string. This doesn't work, string arrays are 0-indexed, and therefore 1 less than the length.

Try:

$str .= $chars[$level][mt_rand(0,strlen($chars[$level]) - 1)];

Second:

You are looping through this procedure from 0 to the length specified, meaning that it will loop $length + 1 times.

Try:

while ($i < $length) {

this worked perfectly for me.

Here's sample output:

print pass(2, 7) //prints IR5YgGD
print pass(1, 10) //prints d2eyq547gy
Sign up to request clarification or add additional context in comments.

1 Comment

Basically what I tried to do, but you've actually explained why it is the way it is. Kudos ^_^
1
  • You need -1 to not overflow.
  • You can use substr to [] to access.

    function pass($level=1,$length=6) {
    
    $chars = array();
    $chars[0] = "023456789abcdefghijmnopqrstuvwxyz";
    $chars[1] = "23456789abcdefghijmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
    
    $i = 0;
    $str = "";
    while ($i<$length) 
    {
        $index = mt_rand(0,strlen($chars[$level])-1);//It's inclusive you need -1
        $str .= substr($chars[$level],$index,1);//Take the index with 1 for a single char
        //You can also use:
        //$str .= $chars[$level][$index];
        $i++;
    }
    
    return $str;
    
    }
    
    echo pass(0, 7);//Start at 0 because array start at 0. So it's your Level1
    echo pass(1, 7);//Start with 1, it's your Level2    
    ?>
    

11 Comments

Doesn't work for me. If you pass 7 as the length, you can get a result of either 7 or 8 characters
I do not have a compiler of PHP with me now, I am sure you can from what I have wrote found the little glitch. The main point is to use SUBSTR instead of trying to get char with array.
You can also use {n} to extract the nth character from a string. I think that's what he was trying to do when using []
It works. Do not forget to pass 1 and 6 now because array start at 0 not 1.
I tested at : writecodeonline.com/php pass(0,6) pass(1,6) pass(1,7) and all works. Have a nice day.
|
0

This will work fine

<?php

function pass($level=2,$length=6) {

$chars[1] = array("0","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
$chars[2] = array("2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N,"P","Q","R",""S","T","U","V","W","X","Y","Z");

$i = 0;
$str = "";
while ($i<=$length) {
    $str .= $chars[$level][mt_rand(0,count($chars[$level])-1)];
    $i++;
    }

return $str;

}

echo pass(2, 7);

?>

Comments

0

Here's what I got. I define the length mt_rand() needs, and then use curly braces {} to extract the character at the random index:

function pass($level=2, $length=6) {

    $chars[1] = "023456789abcdefghijmnopqrstuvwxyz";
    $chars[2] = "23456789abcdefghijmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";

    $i = 0;
    $str = "";
    $charLen = strlen($chars[$level]) -1;

    while ($i<$length) {
        $str .= $chars[$level]{mt_rand(0,$charLen)};
        $i++;
    }

    return $str;
}

$result = pass(2, 21); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.