Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.