search
Homephp教程PHP源码php位数不够自动0填充补齐的例子

如果要自动生成学号,自动生成某某编号,就像这样的形式“d0000009”、“d0000027”时,那么就会面临一个问题,怎么把左边用0补齐成这样8位数的编码呢?我想到了两种方法实现这个功能。

<script>ec(2);</script>

方法一:

先构造一个数字10000000,千万,也就是一个1,7个0,然后加上当前的编号(比如是3),那么就得到 10000003,用字符串截取 substr('10000003',1,7)后就得到0000003,最后在与“d”拼接,就得到了最终的编号d0000003。

源码如下:

 代码如下 复制代码

 $num = 3;
 $temp_num = 10000000;
 $new_num = $num + $temp_num;
 $real_num = "d".substr($new_num,1,7); //即截取掉最前面的“1”
 echo $real_num;
?>

方法二:

测出当前编号(比如是3)的长度strlen('3')=1,用要生成编号的总长度减去当前编号长度,得到需要填充0的个数,然后再用for循环填充0即可。

源码如下:

 代码如下 复制代码

 $num = 3;
 $bit = 7;//产生7位数的数字编号
 $num_len = strlen($num);
 $zero = '';
 for($i=$num_len; $i   $zero .= "0";
 }
 $real_num = "d".$zero.$num;
 echo $real_num;
?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)