GroundZeroStudios Posted March 28, 2007 Share Posted March 28, 2007 All I need to know is how to come up with a random alphanumeric string 24 characters long. Such as Dj53dljgdg97dgfhgf879ngf. It doesn't need to contain upper-case letters. *Alphanumeric means containing letters and numbers Link to comment https://forums.phpfreaks.com/topic/44721-random-alphanumeric-string/ Share on other sites More sharing options...
JBS103 Posted March 29, 2007 Share Posted March 29, 2007 This should work. No clue if it would be considered "good practice" or not. $var = substr(md5(mt_rand()), ; //or $var = substr(md5(time()), ; Link to comment https://forums.phpfreaks.com/topic/44721-random-alphanumeric-string/#findComment-217112 Share on other sites More sharing options...
Lytheum Posted March 29, 2007 Share Posted March 29, 2007 function get_rand_id($length) { if($length>0) { $rand_id=""; for($i=1; $i<=$length; $i++) { mt_srand((double)microtime() * 1000000); $num = mt_rand(1,36); $rand_id .= assign_rand_value($num); } } return $rand_id; } Usage: $id = get_rand_id(24); // Returns random 24 character string. Link to comment https://forums.phpfreaks.com/topic/44721-random-alphanumeric-string/#findComment-217113 Share on other sites More sharing options...
Barand Posted March 29, 2007 Share Posted March 29, 2007 One way is <?php $src = 'abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $len = strlen($src) - 1; $str = ''; for($i=0; $i<24; $i++) { $x = rand(0,$len); $str .= $src{$x}; } echo $str; ?> Link to comment https://forums.phpfreaks.com/topic/44721-random-alphanumeric-string/#findComment-217116 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.