Skip to content

Instantly share code, notes, and snippets.

@pjdietz
Created June 12, 2013 17:51
Show Gist options
  • Save pjdietz/5767560 to your computer and use it in GitHub Desktop.
Save pjdietz/5767560 to your computer and use it in GitHub Desktop.
Create a random string of a given length in PHP
<?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