Jump to content

How to find next numeric/alpha character


dlebowski

Recommended Posts

What I am trying to do is find out what the next value would be if I am using a combination of alpha and numeric characters.  For example, if I have the number 1.  The next value would be 1a, next would be 1b, etc.  When you get to 1z, it would then go 1aa, 1ab, 1ac, etc....

 

Does anyone know how I would accomplish this?  Thank you in advance!

 

Ryan

Link to comment
https://forums.phpfreaks.com/topic/208002-how-to-find-next-numericalpha-character/
Share on other sites

<?php
function nextNumber($previous) {
  if(is_numeric($previous)) {
    $next = $previous.'a';
  } else  if(substr($previous,-1) == 'z') {
    $next = substr($previous,0,-1).'aa';
  } else {
    $lastChr = substr($previous,-1);
    $next = substr($previous,0,-1).++$lastChr;
  }
  return $next;
}

echo nextNumber('1');   // 1a
echo nextNumber('1a');   // 1b
echo nextNumber('1z');   // 1aa
echo nextNumber('1aa');   // 1ab
echo nextNumber('1ab');   // 1ac

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.