PHP - Is there a quick, on-the-fly method to test for a single character string, then prepend a leading zero?
PHP -是否有一个快速的方法来测试单个字符串,然后在前置式0之前进行测试?
Example:
例子:
$year = 11;
$month = 4;
$stamp = $year.add_single_zero_if_needed($month); // Imaginary function
echo $stamp; // 1104
4 个解决方案
#1
359
You can use sprintf: http://php.net/manual/en/function.sprintf.php
您可以使用sprintf: http://php.net/manual/en/function.sprintf.php
<?php
$num = 4;
$num_padded = sprintf("%02d", $num);
echo $num_padded; // returns 04
?>
It will only add the zero if it's less than the required number of characters.
它只会在小于所需字符数时添加零。
Edit: As pointed out by @FelipeAls:
编辑:如@FelipeAls所指出:
When working with numbers, you should use %d
(rather than %s
), especially when there is the potential for negative numbers. If you're only using positive numbers, either option works fine.
当处理数字时,应该使用%d(而不是%s),特别是当可能出现负数时。如果你只使用正数,任何一种都可以。
For example:
例如:
sprintf("%04s", 10);
returns 0010sprintf("%04s", -10);
returns 0-10
sprintf(“% 04年代”,10);0010年返回sprintf(“% 04年代”,-10);返回清廉
Where as:
的地方:
sprintf("%04d", 10);
returns 0010sprintf("%04d", -10);
returns -010
sprintf(“% 04 d”,10);0010年返回sprintf(“% 04 d ",-10);返回-010
#2
150
You can use str_pad
for adding 0's
可以使用str_pad添加0
str_pad($month, 2, '0', STR_PAD_LEFT);
string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )
字符串str_pad (string $input, int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT]])
#3
15
The universal tool for string formatting, sprintf
:
字符串格式通用工具,sprintf:
$stamp = sprintf('%s%02s', $year, $month);
http://php.net/manual/en/function.sprintf.php
http://php.net/manual/en/function.sprintf.php
#4
5
It looks like they take a lot of lines to make it work. I use this short IF functionality and it gives the same result as OP wants.
看起来他们需要很多线才能让它工作。我使用这个简短的IF功能,它提供的结果与OP想要的一样。
$month = 8;
echo ($month < 10 ? '0'.$month : $month); // output: 08
And it takes only 1 line to show the correct output
只需要一行就能显示出正确的输出
#1
359
You can use sprintf: http://php.net/manual/en/function.sprintf.php
您可以使用sprintf: http://php.net/manual/en/function.sprintf.php
<?php
$num = 4;
$num_padded = sprintf("%02d", $num);
echo $num_padded; // returns 04
?>
It will only add the zero if it's less than the required number of characters.
它只会在小于所需字符数时添加零。
Edit: As pointed out by @FelipeAls:
编辑:如@FelipeAls所指出:
When working with numbers, you should use %d
(rather than %s
), especially when there is the potential for negative numbers. If you're only using positive numbers, either option works fine.
当处理数字时,应该使用%d(而不是%s),特别是当可能出现负数时。如果你只使用正数,任何一种都可以。
For example:
例如:
sprintf("%04s", 10);
returns 0010sprintf("%04s", -10);
returns 0-10
sprintf(“% 04年代”,10);0010年返回sprintf(“% 04年代”,-10);返回清廉
Where as:
的地方:
sprintf("%04d", 10);
returns 0010sprintf("%04d", -10);
returns -010
sprintf(“% 04 d”,10);0010年返回sprintf(“% 04 d ",-10);返回-010
#2
150
You can use str_pad
for adding 0's
可以使用str_pad添加0
str_pad($month, 2, '0', STR_PAD_LEFT);
string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )
字符串str_pad (string $input, int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT]])
#3
15
The universal tool for string formatting, sprintf
:
字符串格式通用工具,sprintf:
$stamp = sprintf('%s%02s', $year, $month);
http://php.net/manual/en/function.sprintf.php
http://php.net/manual/en/function.sprintf.php
#4
5
It looks like they take a lot of lines to make it work. I use this short IF functionality and it gives the same result as OP wants.
看起来他们需要很多线才能让它工作。我使用这个简短的IF功能,它提供的结果与OP想要的一样。
$month = 8;
echo ($month < 10 ? '0'.$month : $month); // output: 08
And it takes only 1 line to show the correct output
只需要一行就能显示出正确的输出