How can I generate an incremental id in php mixing numbers, lower and upper letters?
For example, I tried:
$hello = "aaa0";
for ($i=0; $i < 10000; $i++) {
echo $hello++;
echo "<br>";
}
Then, it returns;
aaa0
aaa1
aaa2
...
aaa9
aab0
aab1
I would like to generate strings as:
aaa0
aaa1
aaa2
...
aaaa
aaab
aaac
...
aaaz
aaaA
First numbers from 0 to 9, then chars from a to z, then chars from A to Z. Each positional char should go in this range.
How can I do it?
EDIT: I want every character in the string varies within this range. I want to go from 0 to 9, then a to z, then A to Z. When it ends, the char go to 0, and the char in the left increments in one. For example:
0000
0001
0002
...
0009
000a
000b
...
000y
000z
000A
000B
...
000X
000Z
0010
0011
0012
....
0019
001a
This should work for you:
<?php
$hello = "aaa";
//'aaa0' -> 'aaa9'
for ($count = 0; $count <= 9; $count++)
echo $hello . $count . "<br />";
//'aaaa' -> 'aaaz'
foreach (range('a', 'z') as $char)
echo $hello . $char . "<br />";
//'aaaA' -> 'aaaZ'
foreach (range('A', 'Z') as $char)
echo $hello . $char . "<br />";
?>
EDIT:
This works only with 3 digit's. After you run out of memory for sure.
<?php
$array = array();
$maxLength = 3;
$output = array();
ini_set('memory_limit', '-1');
$time_start = microtime(true);
foreach(range(0, 9) as $number)
$array[] = $number;
foreach(range('a', 'z') as $char)
$array[] = $char;
foreach(range('A', 'Z') as $char)
$array[] = $char;
function everyCombination($array, $arrLength, $size, $perArr = array(), $pos = 0, &$found = array()) {
if ($size == $pos) {
$found[] = vsprintf("%s%s%s", $perArr);
return;
}
for ($count = 0; $count < $arrLength; $count++) {
$perArr[$pos] = $array[$count];
everyCombination($array, $arrLength, $size, $perArr, $pos+1, $found);
}
return $found;
}
$output = everyCombination($array, count($array), $maxLength);
for($count = 0; $count < count($output); $count++)
echo $output[$count] . "<br/>";
echo "DONE!";
$time_end = microtime(true);
$time = $time_end - $time_start;
echo round($time,2) . " s";
?>
Using 0,1-9, a-z and A-Z is "Base 62". Converting from base 10, to base 62 is very easy in PHP.
<?php
echo base_convert(10123, 10,26), "\n";
// outputs: 'ep9'
echo base_convert('ep9', 26, 10), "\n";
// output 10123
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With