Show DevBest [PHP] Random String Generator

cxdy

PHP Developer
Jun 8, 2014
100
27
I made a random string generator in PHP.
You can use this for things like passwords, file names, etc. Anything you can think of.
To change the length of the string, edit the 10 in $length on line one.
Enjoy.

The function:
PHP:
function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}
Then, if you ever want to call it,
PHP:
echo generateRandomString();
 

Macemore

Circumcised pineapples
Aug 26, 2011
1,681
819
All it takes is one line:
PHP:
echo substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$'), 0, 10);
Yeah but his is easier to read and easier to change for adding / removing characters and such
 

cxdy

PHP Developer
Jun 8, 2014
100
27
All it takes is one line:
PHP:
echo substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$'), 0, 10);
Mine can be easily called multiple times whenever needed, as it's a function. You have to copy & paste yours. Set yours to a variable.
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,398
962
Yeah but his is easier to read and easier to change for adding / removing characters and such
Adding/removing characters would be done exactly the same...
Mine can be easily called multiple times whenever needed, as it's a function. You have to copy & paste yours. Set yours to a variable.
Or can just put it as a function:
PHP:
function generateRandomString($length = 10) {
    return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$"), 0, $length);
}

echo generateRandomString();

Although I wouldn't recommend either of these to be used for generating a random password.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
You don't even need to write '0123456789abc' etc etc, you can just do:

PHP:
<?php
function generateRandomString($length=10) {
    return substr(str_shuffle(array_merge(range(0,9), range('a','z'), range('A', 'Z')), 0, $length);
}

echo 'Your random string is: ', generateRandomString();
?>
 

cxdy

PHP Developer
Jun 8, 2014
100
27
You don't even need to write '0123456789abc' etc etc, you can just do:

PHP:
<?php
function generateRandomString($length=10) {
    return substr(str_shuffle(array_merge(range(0,9), range('a','z'), range('A', 'Z')), 0, $length);
}

echo 'Your random string is: ', generateRandomString();
?>
Didn't know about range! Thanks.
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,398
962
You don't even need to write '0123456789abc' etc etc, you can just do:

PHP:
<?php
function generateRandomString($length=10) {
    return substr(str_shuffle(array_merge(range(0,9), range('a','z'), range('A', 'Z')), 0, $length);
}

echo 'Your random string is: ', generateRandomString();
?>
Or even better:
PHP:
function generateRandomString($length = 10) {
return substr(str_shuffle(md5(time())), 0, $length);
}
echo generateRandomString();
But if you want to make it truly random, use /dev/urandom for Unix based systems
 

Users who are viewing this thread

Top