One, the number is 0.
If you want to automatically generate a number, automatically generate So-and-so number, like this form of "d0000009", "d0000027", then you will face a problem, how to the left with 0 to make up the 8-digit code? I think of two ways to implement this function.
Method One:
First construct a number 10000000, tens of millions, that is, a 1, 7 0, and then add the current number (for example, 3), then get 10000003, with a string intercept substr (' 10000003 ', 1,7) after the 0000003, and finally in the "D" stitching, We get the final number d0000003.
The source code is as follows:
Copy Code code as follows:
<?php
$num = 3;
$temp _num = 10000000;
$new _num = $num + $temp _num;
$real _num = "D". substr ($new _num,1,7); Which is to intercept the "1" at the front.
echo $real _num;
?>
Method Two:
Measure the length of the current number (for example, 3) strlen (' 3 ') = 1, with the total length of the number to be generated minus the current number length, the number to fill 0, and then fill 0 with the For loop.
The source code is as follows:
Copy Code code as follows:
<?php
$num = 3;
$bit = 7;//produces a 7-digit number
$num _len = strlen ($num);
$zero = ';
for ($i = $num _len; $i < $bit; $i + +) {
$zero. = "0";
}
$real _num = "D". $zero. $num;
echo $real _num;
?>
Method Three: Several other methods
Copy Code code as follows:
<?php
$sourceNumber = "1";
$newNumber = substr (Strval ($sourceNumber +1000), 1, 3);
echo "$newNumber";
?>
/* This time will appear: 001
If you want to increase the number of digits can be increased by 1000, and then the 3 also increased.
For example: If I want to make up the "4 0" line No. 03, this will be the case. */
Copy Code code as follows:
<?php
$newNumber = substr (Strval ($sourceNumber +100000), 1,5);
?>
/* In fact, to display a total of several numbers, the $sourcenumber+1 after the number of 0, the last number is directly changed to show several numbers. */
Copy Code code as follows:
/*string Str_pad (string $input, int $pad _length [, String $pad _string [, int $pad _type]]) * *
<?php
$input = "Alien";
Echo Str_pad ($input, 10);
Produces "Alien"
Echo Str_pad ($input, ten, "=", str_pad_left);
Produces "-=-=-alien"
Echo Str_pad ($input, "_", Str_pad_both);
Produces "__alien___"
Echo Str_pad ($input, 6, "___");
Produces "Alien_"
?>
/* The length of the padded string. To pad_string the default to the right, if Str_pad_left to the left, str_pad_both both sides of the complement. The next time with Str_pad, after all, is built, certainly faster than the custom. */
Copy Code code as follows:
/*
I don't think it's a good way for you to introduce the method I wrote. The method function is as follows, so that when you want the result 001, the method: Disprepair (' 1 ', 3, ' 0 ')
Function: Complement function
STR: Original string
Type: 0 is a back up, 1 is a forward complement
Len: New string length
msg: Filling character
*/
function Disprepair ($str, $len, $msg, $type = ' 1 ') {
$length = $len-strlen ($STR);
if ($length <1) return $str;
if ($type = = 1) {
$str = Str_repeat ($msg, $length). $str;
} else {
$str. = Str_repeat ($msg, $length);
}
return $str;
}
Two, the string fills, the automatic completion, the automatic complete
When you encounter a string to output a certain length of time, you can use two methods to automatically fill the PHP string, automatic completion.
Method One:
Copy Code code as follows:
$NEWSTR = sprintf ('%05s ', $str);
The sprintf () feature is very flexible, in the format string above, "%05s" indicates that the output is a string of length 5, if the length is insufficient, the left is complete with zero, and if you write "%5s", the default is to fill the space; If you want to use a different character complement, precede the character with a single quotation mark, which is the shape " % ' #5s ' is completed with a well number; Finally, if you want the complement to occur to the right of the string, add a minus sign after the percent symbol, "%-05s."
Method Two:
[code] $CD _no = str_pad (+ + $next _cd_no,8, ' # ', str_pad_left);
Str_pad (String,length,pad_string,pad_type): see the Manual for specific usage.
String required. Specify the string to be populated.
Length required. Specify the length of the new string. If the value is less than the length of the original string, no action is made.
Pad_string Optional. A string that is specified for use in padding. Default is blank.
Pad_type Optional. Specify the side of the padding string.
These two methods are very convenient to realize the automatic complement function of PHP string.