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
- Fill the blanks with characters. 0 words means blank 0. The space is the default value, which means that the space is left.
- The Alignment method. The default value is right-aligned, and the minus table is aligned to the left.
- Field width.
To the minimum width.
- Accuracy. Refers to the number of floating-point digits after the decimal point.
- 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