Canman2005 Posted July 23, 2007 Share Posted July 23, 2007 Hi all I have a code which starts with a letter and then digits follow, an example code looks like D002482 How can I get php to increase the codes number by 1, leaving the letter the same. The above would look like D002483 I tried $codenum = substr('D002482', 1); $codelet = substr('D002482', 0,1); $code2 = $codenum+1; print $codelet.$code2; But that seems to remove the 00 at the start of the code Can anyone help? Thanks in advance Dave Quote Link to comment Share on other sites More sharing options...
dbo Posted July 23, 2007 Share Posted July 23, 2007 Yes it can be done. Do you have any code written yet? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 23, 2007 Share Posted July 23, 2007 look into sprintf() that will pad the 0s for you, but i think you need to do something like <?php $num = 002482 $num = sprintf("%06s", $num+1); $code = "D".$num; echo $code; //Should give you D0024823 ?> Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted July 23, 2007 Author Share Posted July 23, 2007 Hi That gives me D000021. Basically, if the number is 002482 then the next one should be 002483 Does that help? Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.