PHP in front of the number 0 to get fixed-length number of two methods

Source: Internet
Author: User
Tags sprintf string format

Relatively basic, in fact two built-in functions can be implemented.

1 sprintf

Syntax: string sprintf (string format, mixed [args] ...);

return value: string

function Type: data processing

This function is used to format a string. Parameter format is the converted format, starting with the percent sign% to convert characters. The converted format contains the

    1. Fill the blanks with characters. 0 words means blank 0. The space is the default value, which means that the space is left.
    2. The Alignment method. The default value is right-aligned, and the minus table is aligned to the left.

    3. Field width.

      To the minimum width.

    4. Accuracy. Refers to the number of floating-point digits after the decimal point.

    5. Type. See the table below
      % Print percent symbol, do not convert.

      B The integers are converted into binary.

      C The integer is converted to the corresponding ASCII character.

      D Integers are converted to 10.

      F The number of times accuracy is converted into floating point numbers.

      O The integer is turned into octal.
      S The integer is converted into a string.

      X The integer is converted to lowercase 16 rounding.

      X Integers are converted to uppercase 16 rounding.

<?php
$number = 21365478;

$number = sprintf ("%09d", $number);

echo $number. " <br/> ";

?

>

2 Str_pad

语法:

string str_pad (string input, int pad_length [, string pad_string [, int pad_type]])

说明:

This function is padded to the left , right, or left and right edges of the string parameter input, and becomes the specified padding length.

Assume that no non-essential options are provided pad_string. The parameter input is padded with blanks , or otherwise. It will be padded to the specified length using pad_string .

non-essential option Pad_type can be str_pad_right , str_pad_left or Str_pad_both . , assuming that no pad_type is specified, it is assumed that Str_pad_right .

assuming that the value of the pad_length is negative or less than the length of the input string , it won't be padded. .

<?

Php
$input = "Alien";
Echo Str_pad ($input, 10). "                      <br/> "; Produces "Alien"
Echo Str_pad ($input, ten, "-=", str_pad_left). "  <br/> "; Produces "-=-=-alien"
Echo Str_pad ($input, Ten, "_", Str_pad_both). "   <br/> "; Produces "__alien___"
Echo Str_pad ($input, 6, "___"). " <br/> ";

?>

PHP in front of the number 0 to get fixed-length number of two methods

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.