You're looking for the str_pad function.
http://www.php.net/str_pad

$i = 3;
$i = str_pad($i, 5, '0', STR_PAD_LEFT);
echo $i;  // outputs '00003'

Just keep in mind that it won't help you if your input is greater than 5
to begin with, you'll have to check that seperately.

$j = 123456;
$j = str_pad($j, 5, '0', STR_PAD_LEFT);
echo $j; // outputs '123456'


mh.


On Sun, 9 Mar 2003, Vincent M. wrote:

> Hello,
>
> Is there an easy way to change an integer to a 5 caracters string.
> For example:
> $i = 3 ;
> $j = 110 ;
> After having changed them:
> $i = "00003" ;
> $j = "00110" ;
>
> Is there a php function to do so instead of doing this:
>        if( ($i>0)&&($i<10)) {
>       $i = "0000".$i ;
>        } else if( ($i>9)&&($i<100)) {
>       $i = "000".$i ;
>        } else if( ($i>99)&&($i<1000)) {
>       $i = "00".$i ;
>        } etc...
>
> Thanks :-)
>
> Vincent.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to