PHP numeric string left-side Population 0, string population, and automatic population Methods

Source: Internet
Author: User

1. Add 0 to the number.


If you want to automatically generate a student ID and automatically generate a student ID, such as "d0000009" and "d0000027", you will face a problem, how can we merge the left side with 0 into such 8-digit encoding? I have come up with two ways to implement this function.

Method 1:

First construct a number of 10000000, 10 million, that is, a 10000003 0, and then add the current number (for example, 3), then get 10000003, and use a string to intercept substr ('123456 ', 0000003). After splicing with "d", the final number d0000003 is obtained.

The source code is as follows:
Copy codeThe Code is as follows:
<? Php
$ Num = 3;
$ Temp_num = 10000000;
$ New_num = $ num + $ temp_num;
$ Real_num = "d". substr ($ new_num,); // truncates the first "1"
Echo $ real_num;
?>

Method 2:

Measure the length of the current number (for example, 3) strlen ('3') = 1. Use the total length of the number to be generated minus the length of the current number to obtain the number that needs to be filled with 0, then fill in 0 with the for loop.

The source code is as follows:
Copy codeThe Code is as follows:
<? Php
$ 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 codeThe Code is as follows:
<? Php
$ SourceNumber = "1 ";
$ NewNumber = substr (strval ($ sourceNumber + 1000), 1, 3 );
Echo "$ newNumber ";
?>
/* At this time, the following error occurs: 001.
If you want to increase the number of digits, you can increase the number by 1000, and then increase the number by 3.
For example, if I want to add "4 0" 03rd rows, it will become like this. */

Copy codeThe Code is as follows:
<? Php
$ NewNumber = substr (strval ($ sourceNumber + 100000 );
?>
/* In fact, the total number of digits is to be displayed, and the number of digits after $ sourceNumber + 1 is added is 0. The last digit is directly changed to the number of digits to be displayed. */

Copy codeThe Code is 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, 10, "-=", STR_PAD_LEFT );
// Produces "-=-Alien"
Echo str_pad ($ input, 10, "_", STR_PAD_BOTH );
// Produces "_ Alien ___"
Echo str_pad ($ input, 6 ,"___");
// Produces "Alien _"
?>
/* Fill in the length of the string. Pad it with pad_string. It is filled on the right by default. If STR_PAD_LEFT is filled on the left, and STR_PAD_BOTH is supplemented on both sides. The next time we use str_pad, it is built-in and certainly faster than custom. */

Copy codeThe Code is as follows:
/*
I think the above method is not very good. Let's introduce a method I wrote. The method function is as follows, so that when you want the result 001, the method is dispRepair ('1', 3, '0 ')
Function: complement function
Str: original string
Type: type. 0 indicates post-completion, and 1 indicates pre-completion.
Len: New String Length
Msg: Fill in characters
*/
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, Automatic completion, and automatic completion

If you want to output a string of a certain length, you can use either of the following two methods for PHP string auto-filling and auto-completion.

Method 1:

Copy codeThe Code is as follows: $ newStr = sprintf ('% 05s', $ str );

Sprintf () is very flexible. In the preceding Format String, "% 05 s" indicates that the output is a string with a length of 5. If the length is insufficient, the left side is supplemented with zero; if it is written as "% 5s", space is used by default. If you want to use other characters to complete, you must add a single quotation mark before the character, that is, if the expression "% '# 5s" is filled with the well number, and finally, if you want the completion to happen on the right side of the string, add the minus sign after the percent sign, "%-05 s ".

Method 2:

[Code] $ cd_no = str_pad (++ $ next_cd_no, 8, '#', STR_PAD_LEFT );

Str_pad (string, length, pad_string, pad_type): For detailed usage, see the manual.

String is required. Specifies the string to be filled.
Length is required. Specify the length of the new string. If the value is smaller than the length of the original string, no operation is performed.
Pad_string is optional. The specified string for filling. The default value is blank.
Pad_type is optional. Specify the other side of the string to be filled.

These two methods enable PHP string auto-completion.

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.