dlebowski Posted July 17, 2010 Share Posted July 17, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/208002-how-to-find-next-numericalpha-character/ Share on other sites More sharing options...
Mchl Posted July 17, 2010 Share Posted July 17, 2010 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/208002-how-to-find-next-numericalpha-character/#findComment-1087397 Share on other sites More sharing options...
jcbones Posted July 17, 2010 Share Posted July 17, 2010 And to use Mchl's function in a loop to show multiple numbers. $result = 1; for($i = 0; $i < 100; $i++) { $result = nextNumber($result); echo $result . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/208002-how-to-find-next-numericalpha-character/#findComment-1087464 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.