DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • DataWeave: Play With Dates (Part 1)
  • Tired of Messy Code? Master the Art of Writing Clean Codebases
  • The Long Road to Java Virtual Threads

Trending

  • Build a Voice Assistant With Gemini Live API
  • Member Spotlight: Shai Almong
  • Spring Security JWT Validation Best Practices
  • How to Build a Scalable SaaS Product Architecture

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. Increment Numeric Part of String

Increment Numeric Part of String

Ever wondered how to easily increment a number that's part of a string? Check out how to quickly do this in PHP!

By 
Paul Underwood
·
May. 13, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
13.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Have you ever needed to increment the number which is part of a string? For example, if you have an invoice number with the format und001 and want to be able to automatically create the next invoice number of und002 you need to be able to increment the number.

PHP has a built-in way of incrementing either a number or a string simply by placing ++ at the end of the variable.

// Number
$i = 1;

$i++;

// echo 2
echo $i;


But you can also do this for letters; PHP will increment the variable to the next letter in the alphabet.

// Letter
$a = 'a';

$a++;

// echo b
echo $a;


Therefore, if we have an invoice number of und001 then to get the next number, all we have to do is use the ++ syntax to get the next number.

// Invoice
$i = 'und001';

$i++;

// echo und002
echo $i;


A problem can occur if you have a different format such as 001und or invoice-001-und, where the invoice number isn't at the end of the string. You then need to search the string for the number of the invoice and set the increment by one. This is where preg_replace_callback function comes in handy, by searching for digits and replacing the number with a plus one.

function increment($matches)
{
    if(isset($matches[1]))
    {
        $length = strlen($matches[1]);
        return sprintf("%0".$length."d", ++$matches[1]);
    }    
}

$invoice = 'invoice001und';

$invoice =  preg_replace_callback( "|(\d+)|", "increment", $invoice);

echo '<p>'.$invoice.'</p>';


This takes the string, searches for digits inside the string, and passes these digit values into the function increment. In here we return the digit matched plus 1 and return the new number. Now we can increment a number anywhere inside a string.

Strings Data Types

Published at DZone with permission of Paul Underwood. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • DataWeave: Play With Dates (Part 1)
  • Tired of Messy Code? Master the Art of Writing Clean Codebases
  • The Long Road to Java Virtual Threads

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook