EDukeeey Posted January 4, 2011 Share Posted January 4, 2011 Hello, I am attempting to develop a simple php script which will generate a 4 letter sequence when a button is pressed. However the sequence has a few rules it has to follow. For example: the letters have to be A B C D E F G H J K L M P Q and R The second letter must be a letter AFTER the first The fourth letter must be a letter AFTER the third There can't be 2 of the same number. I am a bit confused on how I will do this as I am quite new to php, I though about generating the first as $1, then generating the second as $2, then if $1 > $2, then I would regenerate $2 until it wasn't. Then repeating that for $4. However I am not sure on the coding which would be involved in doing this. If someone could give me a few pointers or a link to a website which would help me it would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/223397-php-letter-sequence-generator/ Share on other sites More sharing options...
smerny Posted January 4, 2011 Share Posted January 4, 2011 the first would have to leave room for the other 3... so it's possibilities would be A B C D E F G H J K L M then seconds possibilities would be from (exclusive) the first letter to (inclusive) P then thirds possibilities would be from (exclusive) the second letter to (inclusive) Q then fourths possibilities would be from (exclusive) the third letter to (inclusive) R you could use an array or just a string... using random to find the position of the letter to use, save that letter... as well as the position so you can create a substring or portion of the array to do the next random... etc Quote Link to comment https://forums.phpfreaks.com/topic/223397-php-letter-sequence-generator/#findComment-1154791 Share on other sites More sharing options...
mikecampbell Posted January 4, 2011 Share Posted January 4, 2011 $ltrs = "ABCDEFGHJKLMPQR"; $one = rand(0, strlen($ltrs)-2); $str = $ltrs[$one]; $ltrs = substr($ltrs, 0, $one).substr($ltrs, $one+1); // remove this letter, it can't be used again $two = rand($one, strlen($ltrs)-1); // must come after $one $str .= $ltrs[$two]; $ltrs = substr($ltrs, 0, $two).substr($ltrs, $two+1); // remove this letter, it can't be used again $three = rand(0, strlen($ltrs)-2); $str = $ltrs[$three]; $ltrs = substr($ltrs, 0, $three).substr($ltrs, $three+1); // remove this letter, it can't be used again $four = rand($three, strlen($ltrs)-1); // must come after $three $str .= $ltrs[$four]; return $str; Quote Link to comment https://forums.phpfreaks.com/topic/223397-php-letter-sequence-generator/#findComment-1154793 Share on other sites More sharing options...
EDukeeey Posted January 4, 2011 Author Share Posted January 4, 2011 I think I may need to learn a bit more for this. Thankyou very much for the code, I tried to implement it into a page using <?php $ltrs = "ABCDEFGHJKLMPQRS"; $name = $_POST['name']; $one = rand(0, strlen($ltrs)-2); $str = $ltrs[$one]; $ltrs = substr($ltrs, 0, $one).substr($ltrs, $one+1); // remove this letter, it can't be used again $two = rand($one, strlen($ltrs)-1); // must come after $one $str .= $ltrs[$two]; $ltrs = substr($ltrs, 0, $two).substr($ltrs, $two+1); // remove this letter, it can't be used again $three = rand(0, strlen($ltrs)-2); $str = $ltrs[$three]; $ltrs = substr($ltrs, 0, $three).substr($ltrs, $three+1); // remove this letter, it can't be used again $four = rand($three, strlen($ltrs)-1); // must come after $three $str .= $ltrs[$four]; return $str; $code = $one.$two.$three.$four; ?> <html> <head> <title>Random Code Generator</title> </head> <body> <?php echo "Ref. Name:" .$name. "<br />"; // prints name echo "Code:" .$code. "<br />"; // prints code ?> </body> </html> $name is a variable from a form which is connected to the php doc. But the page just returned blank. Any more help would be very much appreciated. Sorry for any nooby mistakes I may have made. Quote Link to comment https://forums.phpfreaks.com/topic/223397-php-letter-sequence-generator/#findComment-1154825 Share on other sites More sharing options...
BlueSkyIS Posted January 4, 2011 Share Posted January 4, 2011 because there is a return before the code, stopping it from completing. remove the return. Quote Link to comment https://forums.phpfreaks.com/topic/223397-php-letter-sequence-generator/#findComment-1154826 Share on other sites More sharing options...
EDukeeey Posted January 4, 2011 Author Share Posted January 4, 2011 Thankyou, it fixed it. However now what is returned is Ref. Name:Bob (What I entered into the form) Code:513211 (Changed each time) What can I do to change it from the numbers to letters? Quote Link to comment https://forums.phpfreaks.com/topic/223397-php-letter-sequence-generator/#findComment-1154831 Share on other sites More sharing options...
smerny Posted January 4, 2011 Share Posted January 4, 2011 echo "Code:" .$code. "<br />" change to echo "Code:" .$str. "<br />" Quote Link to comment https://forums.phpfreaks.com/topic/223397-php-letter-sequence-generator/#findComment-1154841 Share on other sites More sharing options...
EDukeeey Posted January 4, 2011 Author Share Posted January 4, 2011 That has given me Ref. Name:Bob Code:HL (changes) Quote Link to comment https://forums.phpfreaks.com/topic/223397-php-letter-sequence-generator/#findComment-1154844 Share on other sites More sharing options...
EDukeeey Posted January 4, 2011 Author Share Posted January 4, 2011 How would I make it echo the full thing? (e.g. BHDK rather than BH) Also, sorry for so many questions, I am new to php and my friend wanted this created. Quote Link to comment https://forums.phpfreaks.com/topic/223397-php-letter-sequence-generator/#findComment-1154853 Share on other sites More sharing options...
EDukeeey Posted January 4, 2011 Author Share Posted January 4, 2011 *Bump*. Quote Link to comment https://forums.phpfreaks.com/topic/223397-php-letter-sequence-generator/#findComment-1154889 Share on other sites More sharing options...
mikecampbell Posted January 4, 2011 Share Posted January 4, 2011 Ooops, little typo. Try now. $ltrs = "ABCDEFGHJKLMPQRS"; $name = $_POST['name']; $one = rand(0, strlen($ltrs)-2); $str = $ltrs[$one]; $ltrs = substr($ltrs, 0, $one).substr($ltrs, $one+1); // remove this letter, it can't be used again $two = rand($one, strlen($ltrs)-1); // must come after $one $str .= $ltrs[$two]; $ltrs = substr($ltrs, 0, $two).substr($ltrs, $two+1); // remove this letter, it can't be used again $three = rand(0, strlen($ltrs)-2); $str .= $ltrs[$three]; $ltrs = substr($ltrs, 0, $three).substr($ltrs, $three+1); // remove this letter, it can't be used again $four = rand($three, strlen($ltrs)-1); // must come after $three $str .= $ltrs[$four]; echo $str; Quote Link to comment https://forums.phpfreaks.com/topic/223397-php-letter-sequence-generator/#findComment-1154913 Share on other sites More sharing options...
EDukeeey Posted January 4, 2011 Author Share Posted January 4, 2011 Perfect! Thankyou all very much for helping me with this, I will learn a bit more before going any further with this so that I know more about what I am doing. thanks again Quote Link to comment https://forums.phpfreaks.com/topic/223397-php-letter-sequence-generator/#findComment-1154921 Share on other sites More sharing options...
sasa Posted January 5, 2011 Share Posted January 5, 2011 or <?php $ltrs = "ABCDEFGHJKLMPQRS"; $ltrs = str_split($ltrs); shuffle($ltrs); for($i=0; $i<3; $i+=2) $str .= $ltrs[$i]<$ltrs[$i+1] ? $ltrs[$i].$ltrs[$i+1] : $ltrs[$i+1].$ltrs[$i]; echo $str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/223397-php-letter-sequence-generator/#findComment-1155058 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.