-4

Need PHP alphanumeric count that starts from A001 and goes uptill Z999

It starts from A0001 and goes on like A002, A003 .....

and after A999 - it changes to B001 and so on till Z999

how can this be done ... ?

Can anyone here help me out ..?

thanks in advance !!

  • 5
    A request for code like this is not actually a question, and is not suitable for Stack Overflow. I recommend you take a look at this — **[Ask]** — and head back when you have a specific programming-related question. – Amal Murali Jul 02 '14 at 18:04
  • 1
    Try something, then ask a question about it. – Joshua Dance Jul 02 '14 at 18:08
  • 2
    This question appears to be off-topic because the poster is simply asking to have code written for them – Mark Baker Jul 02 '14 at 18:12
  • no mate, I dont want the code. Basically I want how can I increment an alphanumeri string, suppose A456 is stored in my database and I want to increment it by one, but if A999 is stored in last entry only then i would want the first alphabet to change since integers have reached the maximum range of 999, did you now got my point. I tried that but couldn't do that.. – Jasdeep Singh Jul 02 '14 at 18:23
  • 1
    It's always best to post the code with which you tried and possibly failed or having problems with errors. When you don't do that, it is normal for people to react like this. SO members like to help, but not when there is nothing to work with. It just leaves a lot of confusion and is unclear as to what to suggest at times. (***"Food for thought"*** on your next question, should there be another). ;-) – Funk Forty Niner Jul 02 '14 at 18:41

1 Answers1

1

You can use two foreach loops with the range function:

foreach (range('A', 'Z') as $letter) {
  foreach (range(1, 999) as $number) {
    echo $letter.str_pad($number, 3, '0', STR_PAD_LEFT)."\n";
  }
}

Edit: If you have a value and want to get the next one, then you can use a function or method like this:

function next_value($current) {
    $letter = $current[0];
    $number = (int) substr($current, 1);

    if ($number == 999) {
      $letter++;
      $number = 1;
    }
    else {
      $number++;
    }

    return $letter.str_pad($number, 3, '0', STR_PAD_LEFT);
}

var_dump(next_value('A459')); // Returns A460
var_dump(next_value('A999')); // Returns B001
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94