vvr
vvr

Reputation: 466

Generate a random string PHP

For generating random string i written code like this But if the function is called with a negative value, or zero, or 1 million it will create lot of problems. So please suggest me better code.

/*
* The following function is for generating a random string value.
* @param unknown_type $length
* @return string $randString
*/
function generateRandomString( $length ) {
    $randString = "";
    srand();
    $characters = "abcdefghijklmnopqrstuvwxyz1234567890";
    /*
    *   build string of random characters
    */ 
    while( strlen( $randString ) < $length ) {
        $randString .= substr( $characters, rand() % (strlen( $characters )),1 );
    }
return $randString;
}

Upvotes: 2

Views: 3055

Answers (4)

Waqar Alamgir
Waqar Alamgir

Reputation: 9978

    /*
    * The following function is for generating a random string value.
    * @param unknown_type $length
    * @return string $randString
    */
    function generateRandomString( $length ) {
        $valueCheck = 99999999;            
        $length = ($length<0)?-$length:$length;
        $length = ($length==0)?rand(0,$valueCheck):$length;
        $length = ($length>$valueCheck)?rand(0,$valueCheck):$length;
        $randString = "";
        srand();
        $characters = "abcdefghijklmnopqrstuvwxyz1234567890";
        /*
        *   build string of random characters
        */ 
        while( strlen( $randString ) < $length ) {
            $randString .= substr( $characters, rand() % (strlen( $characters )),1 );
        }
    return $randString;
    }

Upvotes: 0

Moon
Moon

Reputation: 35365

You can check for proper value of length. If the length specified is not a practicle, you should reset it.

function generateRandomString($length) 
{
    if($length < 1 || $length > 50)
    {
        $length = 25;    
    }

    // do the rest here...
}

Upvotes: 0

alex
alex

Reputation: 490607

To stop the negative number or zero passed as an argument as being a problem, you could use max(1, $length). Alternatively, check for < 1 and throw an exception or return FALSE.

So please suggest me better code.

Here are some suggestions for your code...

function generateRandomString( $length ) {
    $chars = array_merge(range('a', 'z'), range(0, 9));
    shuffle($chars);
    return implode(array_slice($chars, 0, $length));
}

CodePad.

Note that this won't ever have the same character twice in the resulting string.

If you do desire that, you could use something like...

function generateRandomString( $length ) {
    $chars = array_merge(range('a', 'z'), range(0, 9));

    return array_reduce(array_rand($chars, $length), function($str, $key) use ($chars) { 
        return $str.= $chars[$key];
    }, ''); 

}

CodePad.

...or slightly different...

function generateRandomString( $length ) {
    $chars = array_merge(range('a', 'z'), range(0, 9));

    return implode(array_map(function($key) use ($chars) { 
        return $chars[$key];
    }, array_rand($chars, $length)));   

}

CodePad.

Upvotes: 1

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28389

If your only concern is setting limits on length then you can just set your own length, if your minimmum is 5, and the $length is < 5 you just set it to 5, if your max is 30 you can set it to 30 if it's > 30... see?

What is this for? A much better way to generate a random string of digits which is always going to be 32 characters long is to get an md5 out of the current timestamp. That's not guaranteed to be unique, of course, if two users call the code at the same moment, but without additional information as to what you're doing, I'd consider that approach.

Upvotes: 0

Related Questions