public static function increment($string, $style = 'default', $n = 0)
	{
		$styleSpec = isset(self::$incrementStyles[$style]) ? self::$incrementStyles[$style] : self::$incrementStyles['default'];

		// Regular expression search and replace patterns.
		if (is_array($styleSpec[0]))
		{
			$rxSearch = $styleSpec[0][0];
			$rxReplace = $styleSpec[0][1];
		}
		else
		{
			$rxSearch = $rxReplace = $styleSpec[0];
		}

		// New and old (existing) sprintf formats.
		if (is_array($styleSpec[1]))
		{
			$newFormat = $styleSpec[1][0];
			$oldFormat = $styleSpec[1][1];
		}
		else
		{
			$newFormat = $oldFormat = $styleSpec[1];
		}

		// Check if we are incrementing an existing pattern, or appending a new one.
		if (preg_match($rxSearch, $string, $matches))
		{
			$n = empty($n) ? ($matches[1] + 1) : $n;
			$string = preg_replace($rxReplace, sprintf($oldFormat, $n), $string);
		}
		else
		{
			$n = empty($n) ? 2 : $n;
			$string .= sprintf($newFormat, $n);
		}

		return $string;
	}
public static function increment($string, $style = 'default', $n = 0)
	{
		$styleSpec = isset(self::$incrementStyles[$style]) ? self::$incrementStyles[$style] : self::$incrementStyles['default'];

		// Regular expression search and replace patterns.
		if (is_array($styleSpec[0]))
		{
			$rxSearch = $styleSpec[0][0];
			$rxReplace = $styleSpec[0][1];
		}
		else
		{
			$rxSearch = $rxReplace = $styleSpec[0];
		}

		// New and old (existing) sprintf formats.
		if (is_array($styleSpec[1]))
		{
			$newFormat = $styleSpec[1][0];
			$oldFormat = $styleSpec[1][1];
		}
		else
		{
			$newFormat = $oldFormat = $styleSpec[1];
		}

		// Check if we are incrementing an existing pattern, or appending a new one.
		if (preg_match($rxSearch, $string, $matches))
		{
			$n = empty($n) ? ($matches[1] + 1) : $n;
			$string = preg_replace($rxReplace, sprintf($oldFormat, $n), $string);
		}
		else
		{
			$n = empty($n) ? 2 : $n;
			$string .= sprintf($newFormat, $n);
		}

		return $string;
	}
public static function increment ( $ string , $ style = 'default' , $ n = 0 ) { $ styleSpec = isset ( self :: $ incrementStyles [ $ style ] ) ? self :: $ incrementStyles [ $ style ] : self :: $ incrementStyles [ 'default' ] ; // Regular expression search and replace patterns. if ( is_array ( $ styleSpec [ 0 ] ) ) { $ rxSearch = $ styleSpec [ 0 ] [ 0 ] ; $ rxReplace = $ styleSpec [ 0 ] [ 1 ] ; } else { $ rxSearch = $ rxReplace = $ styleSpec [ 0 ] ; } // New and old (existing) sprintf formats. if ( is_array ( $ styleSpec [ 1 ] ) ) { $ newFormat = $ styleSpec [ 1 ] [ 0 ] ; $ oldFormat = $ styleSpec [ 1 ] [ 1 ] ; } else { $ newFormat = $ oldFormat = $ styleSpec [ 1 ] ; } // Check if we are incrementing an existing pattern, or appending a new one. if ( preg_match ( $ rxSearch , $ string , $ matches ) ) { $ n = empty ( $ n ) ? ( $ matches [ 1 ] + 1 ) : $ n ; $ string = preg_replace ( $ rxReplace , sprintf ( $ oldFormat , $ n ) , $ string ) ; } else { $ n = empty ( $ n ) ? 2 : $ n ; $ string .= sprintf ( $ newFormat , $ n ) ; } return $ string ; }
Increments a trailing number in a string.

Used to easily create distinct labels when copying objects. The method has the following styles:

default: "Label" becomes "Label (2)"
dash:    "Label" becomes "Label-2"

@param   string   $string  The source string.
@param   string   $style   The the style (default|dash).
@param   integer  $n       If supplied, this number is used for the copy, otherwise it is the 'next' number.

@return  string  The incremented string.

@since   1.0
Increments a trailing number in a string .