jasonc Posted July 25, 2011 Share Posted July 25, 2011 I am after an easier way to generate a string of letters and numbers that always starts with a letter and must have three letters and three numbers, it can end in either a letter or number. I thought of using a random number and creating a hexdec but i am wanting to use this as a unique reference number and creating a random number and then checking that it has 3 of each and if not random another until it is, and then checking to make sure it does not already exist. this method would make the site hang for ages most likely until it found one to store! so loking for the best method to creating a 3 letter and 3 number code. any ideas ? Quote Link to comment https://forums.phpfreaks.com/topic/242717-how-would-i-generate-a-string-like-a3b34-or-d56ab/ Share on other sites More sharing options...
cunoodle2 Posted July 25, 2011 Share Posted July 25, 2011 How about this order of operations.. 1) Using a random number generator to get 3 letters. I believe this is code for that.. <?php echo base_convert(3, 10, 36); ?> 2) Then store all 3 of these items into an array "Temp_pass". The first character will be the beginning of your new string which I will call "secure_pass". Remove the first character of the array "Temp_pass" which will now just contain 2 letters. 3) After that generate 3 random integers and append them to the end of the array "Temp_pass." 4) Then use a random number generator (1-5) to determine which element is go next in the string of "secure_pass" Remove this element from the array and append it to the end of "secure_pass" 5) Then use a random number generator (1-4) to determine which element is go next in the string of "secure_pass" Remove this element from the array and append it to the end of "secure_pass" etc... Quote Link to comment https://forums.phpfreaks.com/topic/242717-how-would-i-generate-a-string-like-a3b34-or-d56ab/#findComment-1246655 Share on other sites More sharing options...
jasonc Posted July 25, 2011 Author Share Posted July 25, 2011 hey thank you for your help. i have come up with this method but i have no idea how to mix up the $hex string for ($j = 0; $j < 100; $j++) { //$letters = array(); //$numbers = array(); //$letters = array("A","B","C","D","E","F"); //$numbers = array("0","1","2","3","4","5","6","7","8","9"); $letters = range("A", "F"); $numbers = range("0", "9"); $hex = ""; for ($i = 1; $i <= 3; $i++) { $rand_one = rand(0, rand(0, 5)); $rand_two = rand(0, rand(0, 9)); if ($i < 3) { $hex .= $letters[$rand_one]; //echo(" ".$rand_one); } $hex .= $numbers[$rand_two]; //echo(" ".$rand_two." "); } // mixup the $hex string. // add a random letter to $hex string. $rand_three = rand(0, rand(0, 5)); $hex = $letters[$rand_three] . $hex; echo(" " .$hex."<br>"); } Quote Link to comment https://forums.phpfreaks.com/topic/242717-how-would-i-generate-a-string-like-a3b34-or-d56ab/#findComment-1246669 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 Here is one way: <?php function numberGen() { $letters="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $output=substr($letters, rand(0, strlen($letters)-1), 1); for ($x=0; $x<2; $x++) { $rand[$x] = substr($letters, rand(0, strlen($letters)-1), 1); } for ($x=2; $x<5; $x++) { $rand[$x] = rand(0,9); } shuffle($rand); foreach ($rand as $value) { $output.=$value; } return $output; } echo numberGen(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/242717-how-would-i-generate-a-string-like-a3b34-or-d56ab/#findComment-1246703 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 A little more improved: <?php function numberGen() { $output=chr(rand(65, 65+25)); for ($x=0; $x<2; $x++) { $rand[] = chr(rand(65, 65+25)); } for ($x=0; $x<3; $x++) { $rand[] = rand(0,9); } shuffle($rand); foreach ($rand as $value) { $output.=$value; } return $output; } echo numberGen(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/242717-how-would-i-generate-a-string-like-a3b34-or-d56ab/#findComment-1246706 Share on other sites More sharing options...
jasonc Posted July 26, 2011 Author Share Posted July 26, 2011 great thanks Quote Link to comment https://forums.phpfreaks.com/topic/242717-how-would-i-generate-a-string-like-a3b34-or-d56ab/#findComment-1247185 Share on other sites More sharing options...
AbraCadaver Posted July 26, 2011 Share Posted July 26, 2011 Just for fun: $result = array_merge(array_rand(array_flip(range('A', 'Z')), 2), array_rand(array_flip(range(0, 9)), 3)); shuffle($result); $result = implode('', array_merge((array)array_rand(array_flip(range('A', 'Z')), 1), $result)) ; Quote Link to comment https://forums.phpfreaks.com/topic/242717-how-would-i-generate-a-string-like-a3b34-or-d56ab/#findComment-1247436 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.