Generate incremental id of numbers, Upper and Lower letters

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

Upvotes: 7

Views: 1701

Answers (3)

AaA
AaA

Reputation: 3694

base_convert only supports conversion from base2 to base36.

If intension is just incrementing in base62 (0-9 a-z A-Z) then just simple arithmetic increment (the way CPU does it) should be enough.

We increment LSB (least significant bit/digit) by one then if it overflow, increment next LSB until reach MSB (most significant bit/digit). If there is still overflow, add 1 to front (not zero).

Note: Leading zeros has no meaning in arithmetic, thus a 000a is same as a. if you need zero paddings, add it later.

Note: Order of digits in Base62 is 0-9,A-Z then a-z (same as ASCII table). If you want to change it to 0-9, a-z then A-Z you will need to change GMP string

const GMP = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

function NextB62Digit(string $data): string {
    $i = strpos(GMP, $data);
    if (($i !== NULL) && $i != 61 /*strlen(GMP) - 1*/) {
        return GMP[$i + 1];
    } else {
        return "0";
    }
}

function Base62Increment(string $data): string {
    $out = "";
    $overflow = true;
    $i = strlen($data) - 1;
    while ($i > -1) {
        if ($overflow) {
            $thisDigit = NextB62Digit($data[$i]);
            if ($thisDigit !== '0'){
                $overflow = false;
            }
        } else {
            $thisDigit = $data[$i];
        }
        $out = $thisDigit . $out;
        $i--;
    }
    if ($overflow) {
        $out = '1' . $out;
    }
    return $out;
}

test

echo "<pre>";
echo Base62Increment(   "1") . "\n"; //     2
echo Base62Increment(   "9") . "\n"; //     A
echo Base62Increment(   "z") . "\n"; //    10
echo Base62Increment("000a") . "\n"; //     b
echo Base62Increment("000Z") . "\n"; //     a
echo Base62Increment("aaaz") . "\n"; //  aab0
echo Base62Increment("zzzz") . "\n"; // 10000
echo "</pre>";

Upvotes: 0

Rizier123
Rizier123

Reputation: 59701

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

?>

Upvotes: 5

Alister Bulman
Alister Bulman

Reputation: 35169

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

Upvotes: 2

Related Questions