Sudhir
Sudhir

Reputation: 837

How can I make alphanumeric auto increment id in php

Can somebody guide me how I will create alphanumeric id such as AAA001 in php when passing 1. Both number & character will auto increment.

Upvotes: 0

Views: 1777

Answers (2)

Amit Maan
Amit Maan

Reputation: 85

you can try this.

function increament($string){
    $string = preg_split('/(\d+)/', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    $str = $string[0];
    $num = $string[1];
    $num++;
    if($num>999){
        $str++;
        $num = 000;
    }
    return $str.$num;
}

Upvotes: 1

t3chguy
t3chguy

Reputation: 1018

$id = 'AAA001';
echo $id++;

Every time you use echo $id++; it will return the next one in the sequence.

Testing Environment:

<?php

$id = 'AAA000';
for ($i = 1; $i <= 100100; $i++) {
    echo $id++;
}
echo '<br />';
echo $id;

?>

Upvotes: 0

Related Questions