Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-06 20:17:45
Original
1180 people have browsed it

这篇文章主要介绍了php基于str_pad实现卡号不足位数自动补0的方法,对于生成固定位数号码的应用非常具有实用价值,需要的朋友可以参考下

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

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

参数 描述

string 必需。规定要填充的字符串。

length 必需。规定新字符串的长度。如果该值小于原始字符串的长度,,则不进行任何操作。

pad_string 可选。规定供填充使用的字符串。默认是空白。

pad_type

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

可能的值:


示例如下:

复制代码 代码如下:

$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程序设计有所帮助。

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 admin@php.cn
Popular Recommendations
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!