php is based on str_pad to realize the method of automatically filling in 0 for insufficient digits in the card number, str_pad card number
The str_pad function in PHP can help us automatically fill in the blank digits. The str_pad() function fills the string to the specified length.
The str_pad() function pads a string to the specified length.
Grammar
str_pad(string,length,pad_string,pad_type)
Parameters |
Description |
string |
Required. Specifies the string to be filled. |
length |
Required. Specifies the length of the new string. If the value is less than the length of the original string, no operation is performed. |
pad_string |
Optional. Specifies the string used for padding. The default is blank. |
pad_type |
Optional. Specifies the side of the padding string.
参数 |
描述 |
string |
必需。规定要填充的字符串。 |
length |
必需。规定新字符串的长度。如果该值小于原始字符串的长度,则不进行任何操作。 |
pad_string |
可选。规定供填充使用的字符串。默认是空白。 |
pad_type |
可选。规定填充字符串的那边。
可能的值:
- STR_PAD_BOTH - 填充到字符串的两头。如果不是偶数,则右侧获得额外的填充。
- STR_PAD_LEFT - 填充到字符串的左侧。
- STR_PAD_RIGHT - 填充到字符串的右侧。这是默认的。
|
Possible values:
- STR_PAD_BOTH - padding to both ends of the string . If it's not an even number, the right side gets extra padding.
- STR_PAD_LEFT - pad to the left of the string side.
- STR_PAD_RIGHT - padding to the right of the string side. This is the default.
|
An example is as follows:
Copy code The code is 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 result after running is as follows:
Copy code The code is as follows:
Array ( [0] => 0000000001 [1] => 0000000002 [2] => 0000000003 [3] => 0000000004 [4] => 0000000005 [5] => 0000000006 [6] t; 0000000007 [7] => 0000000008 [8] => 0000000009 [9] => 0000000010 )
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/910602.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/910602.htmlTechArticlephp is based on str_pad to realize the method of automatically filling 0 for insufficient card number digits. str_pad card number automatically fills the blank digits in php The str_pad function can help us achieve this. The str_pad() function fills the string...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn