PHP number string left 0, string padding and automatic completion of several methods _php tutorial

Source: Internet
Author: User
first, the number is 0.


If you want to automatically generate a number, automatically generate a number, such as the form of "d0000009", "d0000027", then you will face a problem, how to use the left 0 to make such a 8-digit code? I think of two ways to implement this function.

Method One:

First construct a number 10000000, tens of thousands, that is, a 1, 7 0, and then add the current number (such as 3), then get 10000003, with a string intercept substr (' 10000003 ', 1,7) to get 0000003, and finally in the "D" splicing, You get the final number d0000003.

The source code is as follows:
Copy the Code code as follows:
$num = 3;
$temp _num = 10000000;
$new _num = $num + $temp _num;
$real _num = "D". substr ($new _num,1,7); is to intercept the "1" in the front.
echo $real _num;
?>

Method Two:

The length of the current number (for example, 3) is measured strlen (' 3 ') = 1, minus the current number length with the total length of the number to be generated, the number of 0 to fill, and then the for loop to fill with 0.

The source code is as follows:
Copy the Code code as follows:
$num = 3;
$bit = 7;//produces 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 Three: Several other methods

Copy the Code code 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 the Code code as follows:
$newNumber = substr (Strval ($sourceNumber +100000), 1,5);
?>
/* Actually is to display a number of numbers, the $sourcenumber+1 after the number of 0, the last number directly changed to show a few numbers. */

copy code 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, ten, "-=", str_pad_left);
//produces "-=-=-alien"
Echo Str_pad ($input, Ten, "_", Str_pad_both);
//produces "__alien___"
Echo Str_pad ($input, 6, "___");
//produces "Alien_"
?
/* Fill in the length of the string. The default is the right side, if the str_pad_left is to the left, str_pad_both both sides of the complement. The next time with Str_pad, after all, is built-in, certainly faster than custom. */

Copy the Code code as follows:
/*
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
*/
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;
}


Second, string filling, auto-completion, auto-complete

When it comes to outputting a certain length of string, you can use the two methods to automatically fill the PHP string, auto-complete.

Method One:

Copy the code as follows: $newStr = sprintf ('%05s ', $str);

The function of sprintf () is very flexible, in the format string above, "%05s" means that the output is a string of length 5, if the length is insufficient, left with zero complement, if written as "%5s", the default is a space complement, if you want to use other character completion, the character is preceded by a single quotation mark, that is, " The representation of% ' #5s ' is complete with a well number; Finally, if you want the completion to occur to the right of the string, add a minus sign after the percent symbol, "%-05s".

Method Two:

[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 is required. Specifies the string to populate.
Length required. Specifies the length of the new string. If the value is less than the length of the original string, no action is made.
Pad_string is optional. Specifies the string to use for padding. The default is blank.
Pad_type is optional. Specify the padding string over there.

These two methods are convenient to implement the automatic completion of the PHP string function.

http://www.bkjia.com/PHPjc/768144.html www.bkjia.com true http://www.bkjia.com/PHPjc/768144.html techarticle first, the number of 0. If you want to automatically generate a number, automatically generate a number, such as the form of "d0000009", "d0000027", then you will face a problem, how to use the left ...

  • 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.