Page 1 of 1

adding zeroes in front

Posted: Thu Sep 28, 2006 8:22 am
by byenary
Hello,

Ive created an array form a query which contains numbers like 5 , 45, 2, 846
But i should have it as 00005 , 00045 , 00002, 00846
What kind of instrucitions whould do this in a nice way...?

THx

Posted: Thu Sep 28, 2006 8:26 am
by Benjamin
From the manual.. http://www.php.net/str_pad

Code: Select all

<?php
$input = "Alien";
echo str_pad($input, 10);                      // produces "Alien     "
echo str_pad($input, 10, "-=", STR_PAD_LEFT);  // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH);   // produces "__Alien___"
echo str_pad($input, 6 , "___");               // produces "Alien_"
?>
So...

Code: Select all

echo str_pad($input, 5, "0", STR_PAD_LEFT);
May be close to what your looking for

But even after you pad it, you will need to ensure that it's treated as a string..

Code: Select all

$input = (string) $input;
Otherwise the 0's may be removed..