php based on str_pad to realize the method of automatically filling 0 for insufficient digits of card number

  • 2021-08-03 09:27:24
  • OfStack

Automatically fill in the number of white space in php str_pad function can help us achieve oh str_pad () function to fill the string to the specified length.

The str_pad () function pills the string to the specified length.
Grammar
str_pad(string,length,pad_string,pad_type)

参数 描述
string 必需。规定要填充的字符串。
length 必需。规定新字符串的长度。如果该值小于原始字符串的长度,则不进行任何操作。
pad_string 可选。规定供填充使用的字符串。默认是空白。
pad_type

可选。规定填充字符串的那边。

可能的值:

  • STR_PAD_BOTH - 填充到字符串的两头。如果不是偶数,则右侧获得额外的填充。
  • STR_PAD_LEFT - 填充到字符串的左侧。
  • STR_PAD_RIGHT - 填充到字符串的右侧。这是默认的。
Examples are as follows:
$cardCount = 10;
$arr = array();
for ($i = 1; $i <= $cardCount; $i++) {
$strCard = str_pad($i, 10, '0', STR_PAD_LEFT);
$arr[] = $strCard;
}
print_r($arr);

The output after running is as follows:

Array ( [0] => 0000000001 [1] => 0000000002 [2] => 0000000003 [3] => 0000000004 [4] => 0000000005 [5] => 0000000006 [6] => 0000000007 [7] => 0000000008 [8] => 0000000009 [9] => 0000000010 )

I hope this article is helpful to everyone's PHP programming.


Related articles: