drisate Posted March 10, 2009 Share Posted March 10, 2009 Hey guys i have a number ex: 4 And i need it to be formatted into a minimum of 2 characters. Ex: 04 1. count the numbers of character of the string 2. If 1 number found add 0 in front I was wandering if there was a faster way to do it with an already made php function? Quote Link to comment https://forums.phpfreaks.com/topic/148771-adding-a-0-in-front/ Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 $string = "1"; if (strlen($string) < 2) { $string = "0" . $string; } As far as a faster way or function to do this, I am not sure. You could easily make the above into a function, however. Quote Link to comment https://forums.phpfreaks.com/topic/148771-adding-a-0-in-front/#findComment-781177 Share on other sites More sharing options...
rhodesa Posted March 10, 2009 Share Posted March 10, 2009 come on premiso...i thought you were better then that <?php $num = '1'; printf('%02d',$num); ?> update: if you need to store it in a variable, just use sprint() instead Quote Link to comment https://forums.phpfreaks.com/topic/148771-adding-a-0-in-front/#findComment-781182 Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 come on premiso...i thought you were better then that Not for the past couple weeks. Crazy ass thyroid is acting up and is messing with my brain. It is freaking ridiculous. That and I never really used printf or sprint, good to know Quote Link to comment https://forums.phpfreaks.com/topic/148771-adding-a-0-in-front/#findComment-781187 Share on other sites More sharing options...
rhodesa Posted March 10, 2009 Share Posted March 10, 2009 p.s - that was supposed to be sprintf() printf() and sprintf() are SO powerful. it cleaned my code up so much once I started using them ...and if printf() scares you, you can always use str_pad() in this case Quote Link to comment https://forums.phpfreaks.com/topic/148771-adding-a-0-in-front/#findComment-781219 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.