jd2007 Posted July 15, 2007 Share Posted July 15, 2007 How to generate random letters and numbers with php ? Link to comment https://forums.phpfreaks.com/topic/60054-generating-random-letters-and-numbers/ Share on other sites More sharing options...
metrostars Posted July 15, 2007 Share Posted July 15, 2007 For numbers: rand(0, 15); where 0 is the lowest and 15 is the highest number. For random letters i usually do something like: md5(rand(0, 10000)); OR: str_shuffle(abcdefghijklmnopqrstuvwxyz); is also a possibility. Link to comment https://forums.phpfreaks.com/topic/60054-generating-random-letters-and-numbers/#findComment-298708 Share on other sites More sharing options...
chigley Posted July 15, 2007 Share Posted July 15, 2007 MD5 also contains letters so you can just do an md5 of a random number and use substr() to cut it to the right length! Link to comment https://forums.phpfreaks.com/topic/60054-generating-random-letters-and-numbers/#findComment-298709 Share on other sites More sharing options...
jd2007 Posted July 15, 2007 Author Share Posted July 15, 2007 thanks, guys ! Link to comment https://forums.phpfreaks.com/topic/60054-generating-random-letters-and-numbers/#findComment-298710 Share on other sites More sharing options...
Wuhtzu Posted July 15, 2007 Share Posted July 15, 2007 I always do something like this when creating a string of a certain length comprised of certain characters: //Length of the random string $length = 8; //Specify which characters you want in your string. Here it's letters (a-z) $characters = range(a-z); //Create the string with a while loop $i = 1; while($i <= $length) { $string .= $characters[rand(0,count($characters))]; $i++; } Link to comment https://forums.phpfreaks.com/topic/60054-generating-random-letters-and-numbers/#findComment-298711 Share on other sites More sharing options...
lewis987 Posted July 15, 2007 Share Posted July 15, 2007 or you can use this simple code: $rand1 = rand(3,86); $rand2 = rand(25,66); $random_number = mktime() * $rand1 + $rand1 * $rand2; Link to comment https://forums.phpfreaks.com/topic/60054-generating-random-letters-and-numbers/#findComment-298714 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.