President Obama Posted January 12, 2011 Share Posted January 12, 2011 Had a crack at making my own generator which makes some jumble 32 characters long with letters and numbers. In logic it seemed fine to me. It practice it just does nothing. function generate(){ $abc = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"); $num = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); $result = ""; for ($i = 0; $i < 32; $i++){ $bool = rand(0,1); if ($bool = 1) { $result . $abc[rand(0,25)]; } else { $result . $num[rand(0,9)]; } } echo $result; } Quote Link to comment https://forums.phpfreaks.com/topic/224200-generator-fail/ Share on other sites More sharing options...
mikosiko Posted January 12, 2011 Share Posted January 12, 2011 [OT]: It practice it just does nothing what a great complement/definition to mix with your username!! [/OT] Quote Link to comment https://forums.phpfreaks.com/topic/224200-generator-fail/#findComment-1158426 Share on other sites More sharing options...
QuickOldCar Posted January 12, 2011 Share Posted January 12, 2011 Is a post on that here, not to mention a few others in the forums dealing with random generation and from arrays/lists. http://www.phpfreaks.com/forums/php-coding-help/building-strings/ Quote Link to comment https://forums.phpfreaks.com/topic/224200-generator-fail/#findComment-1158430 Share on other sites More sharing options...
Rifts Posted January 12, 2011 Share Posted January 12, 2011 <?php $length = 32; $characters = '0123456789abcdefghijklmnopqrstuvwxyz'; $string = ''; for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, strlen($characters) -1 )]; } echo $string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/224200-generator-fail/#findComment-1158434 Share on other sites More sharing options...
QuickOldCar Posted January 12, 2011 Share Posted January 12, 2011 or a function <?php function generateString($length) { $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $string = ""; for ($s = 0; $s < $length; $s++) { $string .= $characters[mt_rand(0, strlen($characters))]; } return $string; } //usage $randomstring = generateString(32);//define amount of characters echo $randomstring; ?> Quote Link to comment https://forums.phpfreaks.com/topic/224200-generator-fail/#findComment-1158450 Share on other sites More sharing options...
QuickOldCar Posted January 12, 2011 Share Posted January 12, 2011 oops forgot to add the -1 to define also added more numbers to offset the letters more <?php function generateString($length) { $characters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $string = ""; for ($s = 0; $s < $length; $s++) { $string .= $characters[mt_rand(0, strlen($characters)-1)]; } return $string; } //usage $randomstring = generateString(32);//define amount of characters echo $randomstring; ?> Quote Link to comment https://forums.phpfreaks.com/topic/224200-generator-fail/#findComment-1158473 Share on other sites More sharing options...
QuickOldCar Posted January 12, 2011 Share Posted January 12, 2011 You can even just pull a random md5 if wanted, and do range for length 1 to 32 <?php function uniqueString($length) { $string = substr(md5(uniqid()), 0,$length); return $string; } //usage $random_string = uniqueString(10);//min 1 to max 32 for length echo $random_string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/224200-generator-fail/#findComment-1158493 Share on other sites More sharing options...
President Obama Posted January 13, 2011 Author Share Posted January 13, 2011 ok Thanks guys. I'm always learning new things. This random stuff is a bit tricky to me. Quote Link to comment https://forums.phpfreaks.com/topic/224200-generator-fail/#findComment-1158742 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.