Created
June 12, 2013 17:51
-
-
Save pjdietz/5767560 to your computer and use it in GitHub Desktop.
Create a random string of a given length in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Return a random string of a set number of characters. | |
* | |
* @param int $length The number of characters for the resulting string | |
* @param string $set The set of characters to use for the random string | |
* @param int $repeat Number of times a given character may be repeated in the random string | |
* @return string | |
*/ | |
function randomString( | |
$length, | |
$set = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', | |
$repeat = 10 | |
) { | |
return substr(str_shuffle(str_repeat($set, $repeat)), 0, $length); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment