Hitori Hitori - 1 month ago 8
Javascript Question

incrementing a string. (JS or PHP)

i would like to know if it's possible to do this in javascript or php or anything.

i want to make an invoice number that starts with 001000, now to get the next invoice number i need to increment it by adding

1
, but when i tried to do it in php. it just converts it to a number, which is not good because i need the
00
prefix. and in javascript. it just add the
1
to the end.

i know it could be done using regex but i don't know how.

EDIT: here's the invoice sequence i want to achieve.


  • 001000

  • 001001

  • 001002



and when it reached to
009999
it should be
010000


please help.

SOLVED:
i used str_pad as spencer said.
php
$last_invoice = Quote::all()->last()->invoice + 1; // to convert it to number.
str_pad( ($last_invoice, 6, '0', STR_PAD_LEFT); // fill 0 prefix.

Answer

i used str_pad as @spencermay said. php $last_invoice = Quote::all()->last()->invoice + 1; // to convert it to number. str_pad( ($last_invoice, 6, '0', STR_PAD_LEFT); // fill 0 prefix.