Reputation: 389
I need generate codes from this letters
$a = array(
'B','C','D','F','G','H','J','K','L','M','N','O',
'P','Q','R','S','T','V','W','X','Y','Z','1','2',
'3','4','5','6','7','8','9','0'
);
But there are 2 conditions: every code must be unique and contains 10 letters. I don't want to get random because is unefficient. Instead, I want go for every letter e.g.:
and so on.. Any ideas?
Upvotes: 3
Views: 221
Reputation: 16915
<?php
$length = 3;
$letters = str_split("ABC"); // define your dictionary here
$index = array_fill(0, $length, 0);
$key = $length - 1;
while(true) {
$code = "";
for($i=0;$i<$length;$i++) {
$code .= $letters[$index[$i]];
}
echo $code ."<br/>"; // output code
$index[$key]++;
while(!isset($letters[$index[$key]])) {
$index[$key] = 0;
$key--;
if($key < 0) {
break 2;
}
$index[$key]++;
}
$key = $length - 1;
}
Example:
Upvotes: 3
Reputation: 5207
<?php
$a = array(
'B','C','D','F','G','H','J','K','L','M','N','O',
'P','Q','R','S','T','V','W','X','Y','Z','1','2',
'3','4','5','6','7','8','9','0'
);
function key_increment (&$symbols, $position = null)
{
global $a;
if ($position === null) $position = count($symbols) - 1;
if ($position == -1) return;
$index = $symbols[$position];
$index ++;
if (!isset($a[$index])) {
$v = 0;
key_increment ($symbols, $position-1);
}
$symbols[$position] = $value;
}
function generate($length, $total)
{
global $a;
$symbols = array_fill(0, $length, 0);
for ($i = 0; $i < $total; $i ++) {
$code = '';
foreach ($symbols as $index) {
$code .= $a[$index];
}
echo $code;
echo '<br />';
key_increment($symbols);
}
}
generate(3, 100);
Upvotes: 1
Reputation: 237817
I think that what you actually want is a list of sequential numbers (therefore not random at all) with a rather unconvetional base system. So BBBBBBBBB
is 0
, while BBBBBBBC
is 1
. This isn't hard to do, but obviously you have to code it yourself. Something like this might work:
function generate($num) {
$num = base_convert($num, 10, 32); // convert the number to base 32
$num = str_pad($num, 10, "0", STR_PAD_LEFT); // pad it with zeros to the left
$num = str_replace(array(
'0','1','2','3','4','5','6','7','8','9','a','b',
'c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v'
), array(
'B','C','D','F','G','H','J','K','L','M','N','O',
'P','Q','R','S','T','V','W','X','Y','Z','1','2',
'3','4','5','6','7','8','9','0'
), $num); // replace the normal characters with your custom array
echo $num, "\n";
}
for ($i = 0; $i < 10; $i++) generate($i);
Obviously you could change the 10
in the for
statement to whatever you liked, and insert into a database rather than echo
ing. Obviously 1.6m records would take some time to generate.
The above code gives the following output:
BBBBBBBBBB
BBBBBBBBBC
BBBBBBBBBD
BBBBBBBBBF
BBBBBBBBBG
BBBBBBBBBH
BBBBBBBBBJ
BBBBBBBBBK
BBBBBBBBBL
BBBBBBBBBM
Upvotes: 4
Reputation: 19879
It's probably best that you just use uniqid, especially seeing as you said you'll be generating 1.6 million codes . If you're concerned about the case, just use strtoupper()
echo strtoupper(uniqid());
Generating all of these and inserting them is going to take a fair bit of time.
Upvotes: 0