1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| /**
*
* @param int $l String length (default is 10)
* @param int $spec String complexity :
* 0 = numbers only
* 1 = 0 + lowercase letters.
* 2 = 1 + uppercase letters. This is the default value
* 3 = 2 + some special chars
* @return string
*/
function randomString($l = 10, $spec = 2) {
$chars = '0123456789';
if(0 < $spec)
$chars .= 'abcdefghijklmnopqrstuvwxyz';
if(1 < $spec)
$chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if(2 < $spec)
$chars .= '!:-_+';
$chars_len = strlen($chars) - 1;
$rstring = '';
for ($i = 0; $i < $l; $i++) {
$rstring .= $chars[rand(0, $chars_len)];
}
return $rstring;
} |