Sergei D.
Sergei D.

Reputation: 13

PHP Function Not Running Correctly

<?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?

Upvotes: 1

Views: 65

Answers (4)

blitzmann
blitzmann

Reputation: 7925

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

Upvotes: 0

Patrick Desjardins
Patrick Desjardins

Reputation: 141013

  • 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    
    ?>
    

Upvotes: 1

rockerest
rockerest

Reputation: 10518

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

Upvotes: 1

genesis
genesis

Reputation: 50982

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

?>

Upvotes: 0

Related Questions