ID:               33374
 Comment by:       nickj-phpbugs at nickj dot org
 Reported By:      tiago dot freire at gmail dot com
 Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: GNU/Linux
 PHP Version:      5.0.4
 New Comment:

Please see str_pad, as this already does what you're looking for:
http://php.net/str-pad

Example code:

for ($i=1;$i<=101;$i++) {
   // This gives 'foo0001'
   print "foo" . str_pad($i, 4, "0", STR_PAD_LEFT) . "\n";
}


Previous Comments:
------------------------------------------------------------------------

[2005-06-17 02:26:17] tiago dot freire at gmail dot com

Description:
------------
Suppose I want to build a string with fixed length (for whatever
reason) by appending a number to a string.

//I want strings like 'foo0001','foo0002','foo0010' etc
for ($i=1;$i<1000;$i++)
{
    $string = 'foo'.$i;
}
strval could be useful IF it accepted a second (int) parameter to tell
it: 
    "This number will have <int parameter> characters in string
form,fill it to the left with zeroes"

With this behaviour,this would work:
for ($i=1;$i<1000;$i++)
{
    $string = 'foo'.strval($i,4);//This would give me '0001'
}
What about floats? Another parameter.

Explained by example:
for ($i=1;$i<1000;$i++)
{
    $string = 'foo'.strval($i,4,3);//This would give me '0001.000'
}

For completeness:
$number = 123456789;
$string = 'bignumber: '.strval($number,5);
Will result in the string 'bignumber123456789', since I asked for less
characters than the number has.



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=33374&edit=1

Reply via email to