Nodral Posted June 17, 2011 Share Posted June 17, 2011 Hi I have a series of numbers which I am putting into an array, however I need them all to be 5 digits long. I want to pad out the left side of the shorter ones with zeroes and the easiest way to do this should be sprintf(). However, I've been reading and rereading various tutorials and manuals (inc php.net) and don't really understand how to achieve this. I thought it would be $formatted=sprintf("%05s", $unformatted); however this doesn't work. Hopefully a php guru or just someone who understands it can give me a quick repsonse to this. Cheers Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 17, 2011 Share Posted June 17, 2011 maybe something like this: $formatted=str_pad($unformatted, 5 , "0"); Quote Link to comment Share on other sites More sharing options...
Nodral Posted June 17, 2011 Author Share Posted June 17, 2011 This is not working, is it due to my string being numeric? I'm getting no zeroes added to the front. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 17, 2011 Share Posted June 17, 2011 if your 'string' is numeric, then it's not a string... try converting to string first. Quote Link to comment Share on other sites More sharing options...
Nodral Posted June 17, 2011 Author Share Posted June 17, 2011 Sorted, I had some trailing whitespace that I wasn't aware of. Cheers for the help, your str_pad worked a treat once I'd trimmed it. 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.