ohdang888 Posted August 30, 2008 Share Posted August 30, 2008 how do i create a string of random letters and numbers??????? for instance... rand() only does numbers, and i want both numbers AND letters. Thanks! Link to comment https://forums.phpfreaks.com/topic/122006-solved-random-letters/ Share on other sites More sharing options...
BlueSkyIS Posted August 30, 2008 Share Posted August 30, 2008 i leave out ambiguous characters like 1, l (lower case L), 0, o, etc... i also don't allow duplicates, which you can easily change. function generatePassword ($length = { // start with a blank password $password = ""; // define possible characters $possible = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"; // set up a counter $i = 0; // add random characters to $password until $length is reached while ($i < $length) { // pick a random character from the possible ones $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); // we don't want this character if it's already in the password if (!strstr($password, $char)) { $password .= $char; $i++; } } // done! return $password; } Link to comment https://forums.phpfreaks.com/topic/122006-solved-random-letters/#findComment-629754 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.