Copy CodeThe code is as follows:
$sourceNumber = "1";
$newNumber = substr (Strval ($sourceNumber +1000), 1, 3);
echo "$newNumber";
?>
This time it will appear: 001
If you want to increase the number of digits, you can increase the 1000 and then increase the 3.
Example: If I want to make up the "4 0" line No. 03 It will be like this.
Copy CodeThe code is as follows:
$newNumber = substr (Strval ($sourceNumber +100000), 1,5);
?>
In fact, to show a total number of numbers, the $sourcenumber+1 after the number of 0, the last number directly changed to show a few numbers.
A better approach:
String Str_pad (string $input, int $pad _length [, String $pad _string [, int $pad _type]])
Copy CodeThe code is as follows:
$input = "Alien";
Echo Str_pad ($input, 10);
Produces "Alien"
Echo Str_pad ($input, ten, "-=", str_pad_left);
Produces "-=-=-alien"
Echo Str_pad ($input, Ten, "_", Str_pad_both);
Produces "__alien___"
Echo Str_pad ($input, 6, "___");
Produces "Alien_"
?>
The length of the string to be padded. Pad_string the default to the right, if Str_pad_left to the left, str_pad_both both sides together to fill.
The next time with Str_pad, after all, is built-in, certainly faster than custom.
/*
The method above you I feel not very good, introduce me to write a method. The method function is as follows, so when you want the result 001, the method: Disprepair (' 1 ', 3, ' 0 ')
Function: Complement function
STR: Original string
Type: 0 for post-complement, 1 for pre-complement
Len: New string length
Msg: Fill Character
*/
Copy CodeThe code is as follows:
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;
}
http://www.bkjia.com/PHPjc/318729.html www.bkjia.com true http://www.bkjia.com/PHPjc/318729.html techarticle Copy the 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 ...