Jump to content

how would i generate a string like "A3B34" OR "D56AB"


jasonc

Recommended Posts

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 ?

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...

 

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>");

}

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();
?>

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();
?>

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))

;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.