1. Numbers are supplemented by 0.
If you want to automatically generate a student number, automatically generate a certain number, like this form "d0000009", "d0000027", then you will face a problem, how to fill in the left side with 0 to become like this 8 What about the encoding of digits? I thought of two ways to achieve this function.
Method 1:
First construct a number 10000000, tens of millions, that is, a 1, 7 zeros, and then add the current number (for example, 3), then you will get 10000003, use the string to intercept substr('10000003',1, 7), you get 0000003, and finally splice it with "d" to get the final number d0000003.
The source code is as follows:
Copy code The code is as follows:
$num = 3;
$temp_num = 10000000;
$new_num = $num + $temp_num;
$real_num = "d".substr($new_num,1,7); //That is, intercept the first "1 ”
echo $real_num;
?>
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, get the number of 0s that need to be filled, and then use a for loop to fill it 0 is enough.
The source code is as follows:
Copy code The code is as follows:
$num = 3;
$bit = 7;//Generate 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 3: Several other methods
Copy code The 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 add For the number of digits, you can increase 1000, and then increase 3 as well.
Example: If I want to add "4 zeros" in line 03, it will look like this. */
Copy code The code is as follows:
$newNumber = substr(strval($sourceNumber+100000 ),1,5);
?>
/*In fact, if you want to display how many digits in total, just add as many 0s after $sourceNumber+1, and the last number will be directly changed to how many digits are displayed. number. */
Copy code The code is as follows:
/*string str_pad ( string $input, int $pad_length [, string $pad_string [ , int $pad_type]] )*/
$input = "Alien";
echo str_pad($input, 10);
// produces "Alien "
echo str_pad($input, 10, "-=", STR_PAD_LEFT);
// produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH);
// produces "__Alien___"
echo str_pad($input, 6 , "___");
// produces "Alien_"
?>
/*Complete the length of the string. Pad_string is used to pad. By default, it is padded to the right. If STR_PAD_LEFT, it is padded to the left. STR_PAD_BOTH is padded to both sides together. Next time, use str_pad. After all, it is built-in and will definitely be faster than custom one. */
Copy code The code is as follows:
/*
I don’t think your method above is very good, please introduce me. A way to write. The method function is as follows, so when you want the result 001, method: dispRepair('1',3,'0')
Function: fill function
str: original string
type: type, 0 is back-fill, 1 is front-fill
len: new string length
msg: padding 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;
}
2. String filling, auto-complete, auto-complete
When you need to output a string of a certain length, you can use the following two methods to automatically fill and complete PHP strings.
Method 1:
Copy code The code is as follows:
$newStr= sprintf('%05s', $str);
The function of sprintf() is very flexible. In the above format string, "%05s" means outputting a string with a length of 5. If the length is insufficient, the left side is filled with zeros; if it is written as "%5s", then By default, spaces are used for completion; if you want to use other characters for completion, you must add a single quotation mark before the character, that is, a form such as "%'#5s" means completion with a pound sign; finally, if you want completion to occur in On the right side of the string, add a minus sign after the percent sign, "%-05s".
Method 2:
[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. Specifies the string to be filled.
length Required. Specifies the length of the new string. If the value is less than the length of the original string, no operation is performed.
pad_string Optional. Specifies the string used for padding. The default is blank.
pad_type Optional. Specifies the side of the padding string.
These two methods conveniently implement the automatic completion function of PHP strings.
http://www.bkjia.com/PHPjc/768144.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/768144.htmlTechArticle1. Add 0 to the number. If you want to automatically generate a student number, automatically generate a certain number, like this form "d0000009" or "d0000027", then you will face a problem, how to use the left side...