Return to Snippet

Revision: 2361
at February 4, 2007 13:10 by drwitt


Updated Code
/**
 * Increments a given string by given interval. An optional array with forbidden return values may be passed.
 * @param string $string String to Increment
 * @param int $increment Increment value, 1 by default
 * @param array $forbidden Array with strings the function must not return.
 * @return string Incremented string
 * @uses String_Increment()
 * @author Carsten Witt <[email protected]>
 * @version 20060706-2230
 */
function String_Increment($string, $increment=1, $forbidden=array()) {
	$regex = "(_?)([0-9]+)$";
	ereg($regex, $string, $regs);
	$z = empty($regs) ? '' : $regs[2];
	$neu = ((int) $z) + $increment;
	$string =  ereg_replace((($z=='') ? "$" : $regs[0]."$"), ((string) $regs[1]).((string) $neu), $string);
	if (in_array($string, $forbidden)) $string = String_Increment($string, $increment, $array);
	return $string;
}

Revision: 2360
at February 4, 2007 13:08 by drwitt


Updated Code
/**
 * Increments a given string by given interval. An optional array with forbidden return values may be passed.
 * @param string $string 
 * @param int Increment value, 1 by default
 * @param array Array with strings the function must not return.
 * @return string Incremented string
 * @uses String_Increment()
 * @author Carsten Witt <[email protected]>
 * @version 20060706-2230
 */
function String_Increment($string, $increment=1, $forbidden=array()) {
	$regex = "(_?)([0-9]+)$";
	ereg($regex, $string, $regs);
	$z = empty($regs) ? '' : $regs[2];
	$neu = ((int) $z) + $increment;
	$string =  ereg_replace((($z=='') ? "$" : $regs[0]."$"), ((string) $regs[1]).((string) $neu), $string);
	if (in_array($string, $forbidden)) $string = String_Increment($string, $increment, $array);
	return $string;
}

Revision: 2359
at February 4, 2007 13:08 by drwitt


Initial Code
/**
 * Increments a given string by given interval. An optional array with forbidden return values may be passed.
 * @param string $string 
 * @param int Increment value, 1 by default
 * @param array Array with strings the function must not return.
 * @return string Incremented string
 * @uses String_Increment()
 * @author Carsten Witt <[email protected]>
 * @version 20060706-2230
 */
	function String_Increment($string, $increment=1, $forbidden=array()) {
		$regex = "(_?)([0-9]+)$";
		ereg($regex, $string, $regs);
		$z = empty($regs) ? '' : $regs[2];
		$neu = ((int) $z) + $increment;
		$string =  ereg_replace((($z=='') ? "$" : $regs[0]."$"), ((string) $regs[1]).((string) $neu), $string);
		if (in_array($string, $forbidden)) $string = String_Increment($string, $increment, $array);
		return $string;
	}

Initial URL


Initial Description
This PHP4 function will increment a given string by a given interval. This might be useful when a string has to be unique. An optional array with "forbidden" return values may be passed.
Usage:

Initial Title
Increment String

Initial Tags
php, function

Initial Language
PHP