Increment the number at the end of string using PHP

What is this for?

Last week we were working on an interface where an item can be cloned to create X number of new items. It worked great, but we wanted to take it one step further and automate the generation of a new label for each item that was cloned. If the starting label ended in a number we wanted to increment that number for each new item. This incrementation should only happen for the number at the end of the starting label (no matter how long that number is). We didn’t want any other possible numbers in the label string to change, only the one that may exist at the end of the label.

Time to write some PHP code

After we knew what we wanted to accomplish with the end result, we got started on coming up with a plan for how to make it happen.

Let’s start with a fun label like this to test our functionality:

$starting_label = 'App1 Row97';


And set how many clones we want to create:

$how_many = 10;

Next up, we need to break our label into two pieces: the base portion (everything before the ending number) and the caboose portion (the number at the end of the label). If the original label doesn’t have a number we can just start our incrementing from 1.

$numbers_in_id = countEndingDigits( $starting_label );
// The sample id ends in a number
if ( $numbers_in_id > 0 ) :
	$base_portion = substr( $starting_label, 0, -$numbers_in_id );
	$digits_portion = substr( $starting_label, -$numbers_in_id );	
else :
	$base_portion = $starting_label;
	$digits_portion = '';
endif;

The key to all of this is a countEndingDigits function we created that you can check out below:

function countEndingDigits($string){
	$tailing_number_digits =  0;
	$i = 0;
	$from_end = -1;
	while ( $i < strlen($string) ) :
		if ( is_numeric( substr( $string,$from_end - $i, 1 ) ) ) :
			$tailing_number_digits++;
		else:
			// End our while if we don't find a number anymore
			break;
		endif;
		$i++;
	endwhile;
	return $tailing_number_digits;
}

This function does the work of figuring out how many numbers are at the end of a string.

You’re welcome.

For example:
GreatLabel1 only has 1 digit at the end
Red5Apple874 has 3 digits at the end
Banana1000Orange has 0 digits at the end
Kicking 4 Old School 5 has 1 digit at the end

The function loops over our label (starting at the last character of the text string) looking for digits and doesn’t stop until it finds a non-number character.

Once we know how many digits are in our original label, we can easily break the label into pieces like this using PHP’s substr function:

GreatLabel1 has a base portion ofGreatLabel and digit portion of 1
Red5Apple874 has a base portion of Red5Apple and digit portion of 874
Banana1000Orange has a base portion of Banana1000Orange and no digit portion
Kicking 4 Old School 5 has a base portion of Kicking 4 Old School and a digit portion of 5

Finding the digit portion of our label allows us to easily take that number and add to it as many times as we need to, creating a higher number value.

Here is how we perform our main loop and put the new label together:

 $i = 0;
// Loop through as many times as we need
while( $i < $how_many ) :
	$i++;
	// Set the customer sample id
	$new_sample_id = $base_portion . intval( $digits_portion + $i );
	echo $new_sample_id .'<br />';
endwhile;


In this demo we loop through 10 times and string together a new sample ID from the base portion and our new distinct number and then output it for display.

There are many ways to provide this functionality, and this is one way we came up with for improving the user experience by automatically incrementing the numbers in the item labels. We enjoyed getting creative with a solution for this feature, and if you have an even better solution we would love to see it.

The full code:

$starting_label = 'App1 Row97';
$how_many = 10;


$numbers_in_id = countEndingDigits( $starting_label );
// The sample id ends in a number
if ( $numbers_in_id > 0 ) :
	$base_portion = substr( $starting_label, 0, -$numbers_in_id );
	$digits_portion = abs(substr( $starting_label, -$numbers_in_id ));	
else :
	$base_portion = $starting_label;
	$digits_portion = '';
endif;

$i = 0;
// Loop through as many times as we need
while( $i < $how_many ) :
	$i++;
	// Set the customer sample id
	$new_sample_id = $base_portion . intval( $digits_portion + $i );
	echo $new_sample_id .'<br />';
endwhile;
 

function countEndingDigits($string){
	$tailing_number_digits =  0;
	$i = 0;
	$from_end = -1;
	while ( $i < strlen($string) ) :
		if ( is_numeric( substr( $string,$from_end - $i, 1 ) ) ) :
			$tailing_number_digits++;
		else:
			// End our while if we don't find a number anymore
			break;
		endif;
		$i++;
	endwhile;
	return $tailing_number_digits;
}

The full code above should output this:

App1 Row98

App1 Row99

App1 Row100

App1 Row101

App1 Row102

App1 Row103

App1 Row104

App1 Row105

App1 Row106

App1 Row107