PHP numeric left-side auto-fill 0-PHP source code

Source: Internet
Author: User
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.