这篇文章主要介绍了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程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
