如何用前导 0 填充个位数
我正在循环一个由一位数和两位数组成的数组。
打印这些值时,我需要确保所有值都显示为两位数。
我需要一个解决方案,将零添加到个位数,但保持两位数不变。
换句话说,我想通过添加零将数字字符串“左填充”到至少两位数字。
如何更改代码以呈现值 1 到 9 的前导 0?
<?php foreach (range(1, 12) as $month): ?>
<option value="<?=$month?>"><?=$month?></option>
<?php endforeach ?>
预期结果:
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
I am looping an array of one-digit and two-digit numbers.
When printing these values, I need to ensure that all values are shown as two-digit numbers.
I need a solution to prepend zeros to the single-digit numbers but leave the two-digit numbers unchanged.
In other words, I want to "left pad" a numeric string to a minimum of two digits by adding zeros.
How can I change my code to render leading 0's for values 1 through 9?
<?php foreach (range(1, 12) as $month): ?>
<option value="<?=$month?>"><?=$month?></option>
<?php endforeach ?>
Expected result:
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可能希望将 sprintf 的值保存到变量中以避免多次调用它。
You'd probably want to save the value of
sprintf
to a variable to avoid calling it multiple times.使用
str_pad()
:或
sprintf()
:Use either
str_pad()
:or
sprintf()
:结果:
适用于 PHP 5.3+。自己尝试一下:https://3v4l.org/aE6HO
Result:
Works in PHP 5.3+. Try it yourself: https://3v4l.org/aE6HO
使用sprintf
Use sprintf
if($month < 10) echo '0' 。 $month;
或
if($month < 10) $month = '0' 。 $月;
if($month < 10) echo '0' . $month;
or
if($month < 10) $month = '0' . $month;
这是一个单行代码,无需明确使用
foreach()
即可很好地完成这项工作。结果是:
Here is a one-liner that does the job nicely without using
foreach()
expressly.With the result of:
在构建 HTML 标记时,我不喜欢在 PHP 标签中跳进跳出。通过在
printf()
中包含选项标签,您永远不需要离开 PHP。此外,没有理由在选项标记中声明
value
属性,因为它与选项的文本值相同。即使没有value
属性,JavaScript 仍然能够从文本中获取标签的value
。仅当选项的
value
属性与选项文本不同时才声明。因此,如果将
value
属性保留为未填充的整数,则声明它是合适的,如下所示:更优雅,当多次使用同一变量时,使用显式的“argnum”(参数编号),后跟文字美元符号,以便变量仅传递到 printf() 一次。
单引号字符串(呈现为一行 HTML): (Demo)
双引号字符串(注意转义 <
d
之前的 code>$): (演示)A Nowdoc 字符串:( 演示)
I don't like bouncing in and out of PHP tags when building my HTML markup. By including the option tags inside
printf()
, you never need to leave PHP.Furthermore, there is no reason to declare the
value
attribute in your option tags because it is identical to the option's text value. Even without thevalue
attribute, JavaScript will still be able to grab thevalue
of the tag from the text.Only declare the option's
value
attribute if is different from the option text.Therefore, it would be appropriate to declare the
value
attribute if you left it as an un-padded integer like this:More elegantly, when using the same variable more than once, use an explicit "argnum" (argument number) followed by a literal dollar sign so that the variable is only passed into
printf()
once.Single-quoted string (rendered as one line of HTML): (Demo)
Double-quoted string (notice escaped
$
befored
): (Demo)A Nowdoc string: (Demo)