PHP Digital left auto-complement 0_php tutorial

Source: Internet
Author: User
Copy CodeThe 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 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 CodeThe code is as follows:
$newNumber = substr (Strval ($sourceNumber +100000), 1,5);
?>

In fact, to show a total number of numbers, the $sourcenumber+1 after the number of 0, the last number directly changed to show a few numbers.

A better approach:
String Str_pad (string $input, int $pad _length [, String $pad _string [, int $pad _type]])

Copy CodeThe code is as follows:
$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_"
?>


The length of the string to be padded. Pad_string the default to the right, if Str_pad_left to the left, str_pad_both both sides together to fill.
The next time with Str_pad, after all, is built-in, certainly faster than custom.
/*
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
*/
Copy CodeThe code is as follows:
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;
}

http://www.bkjia.com/PHPjc/318729.html www.bkjia.com true http://www.bkjia.com/PHPjc/318729.html techarticle Copy the code as follows: PHP $sourceNumber = "1"; $newNumber =substr (Strval ($sourceNumber +1000), 1,3); echo "$newNumber";? This time will appear: 001 If you want to increase the number of digits can ...

  • Related Article

    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.