Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Several methods of filling on the left side of PHP digital string, string filling and automatic filling
Several methods of filling on the left side of PHP digital string, string filling and automatic filling
This article mainly introduces several methods of adding to the left side of PHP numeric strings, string filling and automatic filling. Friends who need it can refer to the following

1. Adding to numbers.

If you want to automatically generate student numbers and so-and-so numbers, such as "d9" and "d27", you will face a problem. How to put them? I thought of two ways to realize this function.

Method 1:

First, construct a number of 1, ten million, that is, a number of 1 and 7 zeros, and then add the current number (for example, 3) to get 13, then intercept substr('13',1,7) with a string to get 3, and finally.

method 2:

measure the length of the current number (for example, 3) strlen('3')=1, subtract the length of the current number from the total length of the number to be generated to get the number of zeros to be filled, and then fill the zeros with for loop.