PHP Code

/*
Purpose
	Used to increment a string, character-by-character.
	This can be used for generating unique sequential IDs based from fixed character sets.
Parameters
	Str (String) = This is the string we want to increment.
	CharSet (String) = This is a list of all characters that are valid in the returned ID.
		These characters are specified in the order in which they are to be used.
		Characters on the left are used first.
Return (String)
	Returns the specified string incremented by 1.
Version History
	1.0 6/4/2007 8:53pm Tested by Russ Tanner on PHP 5.1.
	1.1 6/13/2008 Fixed problem where the string was not incrementing properly.
*/
public function Str_Inc($Str, $CharSet) {

	$r = str_split($Str);	   // split the specified string into an array
	$r = array_reverse($r);	 // make the LS have the zero index
	$carry = false;			 // carry flag
	$imax = count($r) - 1;	  // the last valid index of the array

	// Get the highest and lowest character in the character set
	$HiChar = substr($CharSet, strlen($CharSet) - 1, 1);
	$LoChar = substr($CharSet, 0, 1);
	$OneChar = substr($CharSet, 1, 1); // we need the 1 value for when the odometer flips

	$i = 0; // initialize the index counter

	// Main Loop
	while (true) {

		// Trap: The index is beyond the last character in the string
		if ($i > $imax) {
			// The carry flag is set
			if ($carry) {
				if ($imax > 0) $r = array_fill(0, $imax + 1, $LoChar);  // the odometer is flipping
				$r[] = $OneChar;	// now add a new MS character to the value
				break;  // value is incremented, nothing else to do, exit the loop now
			}
			// At this point, we know the index is beyond the last character in the string, but
			// the carry flag is not set. This means that this must be the first iteration of this
			// loop and that the specified string (Str) is zero-length. In this case, we just
			// add a LS character to the return buffer and exit now.
			$r[] = $OneChar;		// now add a new MS character to the value
			break;				  // value is incremented, nothing else to do, exit the loop now
			// As an optimization, we could merge these two lines with the previous identical ones
			// removing them from the IF block.
		}

		// The current character is not the hi character. Just increment it and we're done.
		if ($r[$i] != $HiChar) {
			$r[$i] = $this->Str_Char_NextChar($r[$i], $CharSet); // increment the current character of the return buffer
			break; // now, exit the loop where the string will be reassembeled and returned
		}

		// At this point we know that the current character is the hi character
		$r[$i] = $LoChar;	   // flip this character back to "zero"
		$carry = true;		  // set the carry flag

		// Now, just increment the index
		$i += 1;

	}

	// Reassemble the array into a string and return it
	$r = array_reverse($r);	 // put the array back into its original order
	return implode('', $r);	 // convert it back into a string

}
 


The Captian
Today they call you "crazy". Tomorrow they call you "ahead of your time."
Global Skywatch Learn about Chemtrails - You're breathing them now!
OnlyTheBestHerbs.com World-class supplements
Mercury Talk Why you are sick.
OneUp Domains Domains, Hosting, Email
1-800-358-4278 (U.S. & Canada)