如何用前导 0 填充个位数

发布于 2024-08-12 23:26:34 字数 896 浏览 9 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

独守阴晴ぅ圆缺 2024-08-19 23:26:34
<?php foreach (range(1, 12) as $month): ?>
  <option value="<?= sprintf("%02d", $month) ?>"><?= sprintf("%02d", $month) ?></option>
<?php endforeach?>

您可能希望将 sprintf 的值保存到变量中以避免多次调用它。

<?php foreach (range(1, 12) as $month): ?>
  <option value="<?= sprintf("%02d", $month) ?>"><?= sprintf("%02d", $month) ?></option>
<?php endforeach?>

You'd probably want to save the value of sprintf to a variable to avoid calling it multiple times.

勿忘初心 2024-08-19 23:26:34

使用 str_pad()

echo str_pad($month, 2, '0', STR_PAD_LEFT);

sprintf()

echo sprintf('%02d', $month);

Use either str_pad():

echo str_pad($month, 2, '0', STR_PAD_LEFT);

or sprintf():

echo sprintf('%02d', $month);
誰ツ都不明白 2024-08-19 23:26:34
array_map( function( $day ) {
    return str_pad( $day, 2, '0', STR_PAD_LEFT );
}, range(1, 31) );

结果:

Array
(
    [0] => 01
    [1] => 02
    [2] => 03
    [3] => 04
    [4] => 05
    [5] => 06
    [6] => 07
    [7] => 08
    [8] => 09
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 13
    [13] => 14
    [14] => 15
    [15] => 16
    [16] => 17
    [17] => 18
    [18] => 19
    [19] => 20
    [20] => 21
    [21] => 22
    [22] => 23
    [23] => 24
    [24] => 25
    [25] => 26
    [26] => 27
    [27] => 28
    [28] => 29
    [29] => 30
    [30] => 31
)

适用于 PHP 5.3+。自己尝试一下:https://3v4l.org/aE6HO

array_map( function( $day ) {
    return str_pad( $day, 2, '0', STR_PAD_LEFT );
}, range(1, 31) );

Result:

Array
(
    [0] => 01
    [1] => 02
    [2] => 03
    [3] => 04
    [4] => 05
    [5] => 06
    [6] => 07
    [7] => 08
    [8] => 09
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 13
    [13] => 14
    [14] => 15
    [15] => 16
    [16] => 17
    [17] => 18
    [18] => 19
    [19] => 20
    [20] => 21
    [21] => 22
    [22] => 23
    [23] => 24
    [24] => 25
    [25] => 26
    [26] => 27
    [27] => 28
    [28] => 29
    [29] => 30
    [30] => 31
)

Works in PHP 5.3+. Try it yourself: https://3v4l.org/aE6HO

如梦亦如幻 2024-08-19 23:26:34
$month = 1;
echo sprintf("%02d", $month);
out: 01

使用sprintf

$month = 1;
echo sprintf("%02d", $month);
out: 01

Use sprintf

时光磨忆 2024-08-19 23:26:34

if($month < 10) echo '0' 。 $month;

if($month < 10) $month = '0' 。 $月;

if($month < 10) echo '0' . $month;

or

if($month < 10) $month = '0' . $month;

栀梦 2024-08-19 23:26:34

这是一个单行代码,无需明确使用 foreach() 即可很好地完成这项工作。

$options = array_map( function( $hour ) {
    return str_pad( $hour, 2, '0', STR_PAD_LEFT );
}, array_combine( range(1,12), range(1,12) ) );

结果是:

Array
(
    [1] => 01
    [2] => 02
    [3] => 03
    [4] => 04
    [5] => 05
    [6] => 06
    [7] => 07
    [8] => 08
    [9] => 09
    [10] => 10
    [11] => 11
    [12] => 12
)

Here is a one-liner that does the job nicely without using foreach() expressly.

$options = array_map( function( $hour ) {
    return str_pad( $hour, 2, '0', STR_PAD_LEFT );
}, array_combine( range(1,12), range(1,12) ) );

With the result of:

Array
(
    [1] => 01
    [2] => 02
    [3] => 03
    [4] => 04
    [5] => 05
    [6] => 06
    [7] => 07
    [8] => 08
    [9] => 09
    [10] => 10
    [11] => 11
    [12] => 12
)
原来分手还会想你 2024-08-19 23:26:34

在构建 HTML 标记时,我不喜欢在 PHP 标签中跳进跳出。通过在 printf() 中包含选项标签,您永远不需要离开 PHP。

foreach (range(1, 12) as $month) {
    printf('<option>%02d</option>', $month);
}

此外,没有理由在选项标记中声明 value 属性,因为它与选项的文本值相同。即使没有 value 属性,JavaScript 仍然能够从文本中获取标签的 value

仅当选项的 value 属性与选项文本不同时才声明。

因此,如果将 value 属性保留为未填充的整数,则声明它是合适的,如下所示:

foreach (range(1, 12) as $month) {
    printf('<option value="%d">%02d</option>', $month, $month);
}

更优雅,当多次使用同一变量时,使用显式的“argnum”(参数编号),后跟文字美元符号,以便变量仅传递到 printf() 一次。

单引号字符串(呈现为一行 HTML): (Demo)

printf('<option value="%1$d">%1$02d</option>', $month);

双引号字符串(注意转义 < d 之前的 code>$): (演示)

printf("<option value=\"%1\$d\">%1$02d</option>\n", $month);

A Nowdoc 字符串:( 演示

$format = <<<'HTML'
<option value="%1$d">%1$02d</option>

HTML;

foreach (range(1, 12) as $month) {
    printf($format, $month);
}

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.

foreach (range(1, 12) as $month) {
    printf('<option>%02d</option>', $month);
}

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 the value attribute, JavaScript will still be able to grab the value 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:

foreach (range(1, 12) as $month) {
    printf('<option value="%d">%02d</option>', $month, $month);
}

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)

printf('<option value="%1$d">%1$02d</option>', $month);

Double-quoted string (notice escaped $ before d): (Demo)

printf("<option value=\"%1\$d\">%1$02d</option>\n", $month);

A Nowdoc string: (Demo)

$format = <<<'HTML'
<option value="%1$d">%1$02d</option>

HTML;

foreach (range(1, 12) as $month) {
    printf($format, $month);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文