This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Case-Sensitive Alphanumeric Increment Function

By: a guest on Nov 24th, 2010  |  syntax: PHP  |  size: 1.26 KB  |  views: 203  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**************
  2. This code is public domain.
  3.  
  4. This function increments a case-sensitive alphanumeric
  5. number.  It will use as many digits as you give it.  If
  6. it tries to increment a number that is at it's maximum
  7. (eg "ZZ"), it returns boolean false.
  8. **************/
  9.  
  10. define('NUMBER_START', ord('0'));
  11. define('NUMBER_END', ord('9'));
  12. define('LOWER_START', ord('a'));
  13. define('LOWER_END', ord('z'));
  14. define('UPPER_START', ord('A'));
  15. define('UPPER_END', ord('Z'));
  16.  
  17. function incrementNumber($number) {
  18.         if (strlen($number) == 0)
  19.                 return false;
  20.        
  21.         $number = str_split($number);
  22.         $last = ord(array_pop($number));
  23.         switch ($last) {
  24.                 case NUMBER_END:
  25.                         $last = LOWER_START;
  26.                         break;
  27.                 case LOWER_END:
  28.                         $last = UPPER_START;
  29.                         break;
  30.                 case UPPER_END:
  31.                         $last = NUMBER_START;
  32.                         $inc = incrementNumber(implode($number));
  33.                         if ($inc === false)
  34.                                 return false;
  35.                         $number = str_split($inc);
  36.                         break;
  37.                 default:
  38.                         $last++;
  39.         }
  40.         $last = chr($last);
  41.         $number[] = $last;
  42.         return implode($number);
  43. }
  44.  
  45. // Usage
  46. echo incrementNumber('0000'); // outputs "0001"
  47. echo incrementNumber('0aB4'); // outputs "0aB5"
  48. echo incrementNumber('0aB9'); // outputs "0aBa"
  49. echo incrementNumber('0aBz'); // outputs "0aBA"
  50. echo incrementNumber('0aBZ'); // outputs "0aC0"
clone this paste RAW Paste Data