Is there any native PHP function which would allow me to do this?
开发者_开发知识库$number = functionName(1, 9999); // $number == 0001
$number = functionName(10, 99); // $number == 10
$number = functionName(10, 999); // $number == 010
If there isn't native function, is there any user-based function?
echo str_pad($value, strlen($max), "0", STR_PAD_LEFT);
for example
$value = 10;
$max = 999;
echo str_pad($value, strlen($max), "0", STR_PAD_LEFT);
// returns 010
See http://ideone.com/KPBV2 and str_pad()
use str_pad its easy! here's the doc
http://php.net/manual/en/function.str-pad.php
Not exactly, but you can write one easily using str_pad
and its pad_type of STR_PAD_LEFT
.
There sure is: str_pad()
<?php
echo str_pad(33, 5, "0", STR_PAD_LEFT);
echo str_pad(1, 5, "0", STR_PAD_LEFT);
echo str_pad(1258, 5, "0", STR_PAD_LEFT);
echo str_pad(89965, 5, "0", STR_PAD_LEFT);
?>
精彩评论