PHP numeric 0 on the left
PHP numeric 0 on the left
Example: employee ID X001
Although the MySQL field is set to the zerofill attribute, it will add 0 to the part with insufficient digits, but this function can only be used on the field with digits. So we had to rely on PHP to do it when necessary.
/* In this case, 001 can increase the number of digits by 1000 and 3. For example, if I want to add "4 0" 03rd rows, it will become like this. */
2. [code] [PHP] code
/* In fact, if you want to display a few digits in Total, add the number of zeros after $ sourceNumber + 1, and change the last digit to the number of digits to display.
3. [code] [PHP] code
/* String str_pad (string $ input, int $ pad_length [, string $ pad_string [, int $ pad_type]) */
/* Fill in the length of the string. pad it with pad_string. default value
4. [code] [PHP] code
/* I think the above method is not very good. let's introduce a method I wrote. The method function is as follows. in this way, if you want the result 001, the function of the method is dispRepair ('1', 3, '0'): the complement function str: original string type: type, 0 is post-completion, 1 is pre-completion len: New string length msg: Fill 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 ;}
The above is the content of the PHP numeric 0 on the left. For more information, see PHP Chinese network (www.php1.cn )!