We help IT Professionals succeed at work.

Incrementing alpha-numerical characters

ray-solomon
ray-solomon asked
on
470 Views
Last Modified: 2008-04-24
What would be a good way to increment alpha-numerical characters? I could not think of a way to do this reliably or find a similar example somewhere. Like splitting up the alpha and numeric characters while incrementing the number and joining them together.

like this:
A1
A2
A3
A4...stay in A's until it reaches A999999

B1
B2
B3
B4...stay in B's until it reaches B999999


and so on until it gets to Z999999.

The goal is to put in a function, then I could give it a proper value and it would return the next incremented value.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2004
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Wow, thank you Zyloch. That is great!
I see there is already a solution, but since I took the time to write my own, I'll post it here anyway:

function nextValue($currentValue) {
      if ($currentValue = 'Z999999')
            return true;
      if (!preg_match('/^([A-Z])(\\d{1,6})$/s',$currentValue,$matches))
            return false;
      if ($matches[2]=='999999')
            return chr(ord($matches[1])+1) . '1';
      return $matches[1] . (string) ((integer) $matches[2] + 1);
}

returns true when it reaches 'Z999999', returns false when the input is invalid, and returns your desired string otherwise.

Author

Commented:
Thank you JamesCssl for your contribution.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.