php基于str_pad实现卡号不足位数自动补0的方法,str_pad卡号_PHP教程

WBOY
Release: 2016-07-13 10:14:06
Original
1085 people have browsed it

php基于str_pad实现卡号不足位数自动补0的方法,str_pad卡号

自动补足空白位数在php中str_pad函数可以帮我们实现哦,str_pad() 函数把字符串填充为指定的长度。

str_pad() 函数把字符串填充为指定的长度。
语法
str_pad(string,length,pad_string,pad_type)

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

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

可能的值:

  • STR_PAD_BOTH - 填充到字符串的两头。如果不是偶数,则右侧获得额外的填充。
  • STR_PAD_LEFT - 填充到字符串的左侧。
  • STR_PAD_RIGHT - 填充到字符串的右侧。这是默认的。

示例如下:
复制代码 代码如下:
$cardCount = 10;
$arr = array();
for ($i = 1; $i $strCard = str_pad($i, 10, '0', STR_PAD_LEFT);
$arr[] = $strCard;
}
print_r($arr);

运行后输出结果如下:
复制代码 代码如下:

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

希望本文所述对大家的PHP程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/910602.htmlTechArticlephp基于str_pad实现卡号不足位数自动补0的方法,str_pad卡号 自动补足空白位数在php中str_pad函数可以帮我们实现哦,str_pad() 函数把字符串填充...
Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!