Jump to content

PHP Letter Sequence Generator


EDukeeey

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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;

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.