Often I find that generating a random string can be tedious, there seem to be so many ways to do this.
I wrote this simple function to do the work, and it can be easily modified.
<?php
/*
* Create a random string
* @author XEWeb <>
* @param $length the length of the string to create
* @return $str the string
*/
function randomString($length = 6) {
$str = "";
$characters = array_merge(range('A','Z'), range('a','z'), range('0','9'));
$max = count($characters) - 1;
for ($i = 0; $i < $length; $i++) {
$rand = mt_rand(0, $max);
$str .= $characters[$rand];
}
return $str;
}
You can remove any of the ranges in $characters (for example if you didn't want uppercase letters delete the range('A','Z')
) or put in your own array of characters.