I think I want to use printf(); but not sure.
My problem is that I need to output a number that has 5 digits. I am grabing a number from a db query and if that number is say 342 I want to add 0's infront of that number so that it becomes 00342.
Can printf() do that??
if so - could I also be directed how I might add a space between each number?
0 0 3 4 2
Thanks for any help,
I think you can do it with (s)printf() but str_pad() should work just as well:
$string = '342';
$string = str_pad($string, 5, '0', STR_PAD_LEFT);
echo $string; // 00342
To add the spaces, try this:
$string = implode(" ", str_split($string));
Here's the (s)printf alternative.
$num = sprintf('%05d', 342); #00342
$num = implode(' ', str_split($num)); #0 0 3 4 2