sprintf, printf output formatted string.
For example, the function prototype for sprintf () is as follows:
String sprintf ( string $format
[, mixed $args
[, mixed $...
]])
Where $format is used to specify the format of the output string.
In summary $format adhere to the following prototypes:
%[n$][flags][width][.precision]specifier
which
- n$ is position specifier, indicating which parameter this placeholder represents
<? PHP $num = 5; $location = ' tree '; $format = ' The%2$s contains%1$d monkeys '; Echo sprintf ($format$num$location);
- Flags are flags that indicate whether the + sign, the fill character, and the alignment are displayed. The specific flags are shown in the table below
Flag |
Describe |
+ |
By default, the sign bit '-' is displayed only if the number is negative. If the number is positive, the symbol bit ' + ' is not displayed. After this flag is set, the sign bit is displayed regardless of whether the number is positive or negative. |
' Sign or 0 |
This flag is used to set the symbol used to fill. The fill symbol is the length specified in order to be the output string to reach width. The default padding is a space. The standard way to specify a shim is a single quote + shim, but for 0 as a filler, The standard definition can be used, or it can be stated directly. |
- |
This flag is used to indicate whether the result of the output is left-justified or right-justified. The lack of savings is right-aligned, stating that the flag is left-justified. |
<?PHPEcho sprintf("|%+4d|%+4d|\n", 1,-1);Echo sprintf("|%-4d|%-4d|\n", 1,-1);Echo sprintf("|%+-4d|%+-4d|\n", 1,-1);/*outputs:| +1| -1| | 1 |-1 | | +1 |-1 |*/Echo sprintf("|%0 4d|\n ",-2);Echo sprintf("|% ': 4d|\n",-2);Echo sprintf("|%-': 4d|\n",-2);/*outputs:|-002| |::-2| | -2::|*/
- Width indicates how many characters the output of this format is at least. That is, the output length of the specified character. See the example above.
- . Precision indicates that several decimals should be retained for floating-point numbers
<? PHP $money = 123.1234; Echo sprintf $money); // 123.1
- specifierSpecifier indicates what parameter type should be treated with the parameter.
<?PHP$n= 43951789;$u=-43951789;$c= 65;//ASCII-is ' a '//notice the double percent, this prints A literal '% ' characterprintf("%%b = '%b ' \ n",$n);//binary Representationprintf("%%c = '%c ' \ n",$c);//Print the ASCII character, same as Chr () functionprintf("%%d = '%d ' \ n",$n);//Standard integer representationprintf("%%e = '%e ' \ n",$n);//Scientific notationprintf("%%u = '%u ' \ n",$n);//unsigned integer representation of a positive integerprintf("%%u = '%u ' \ n",$u);//unsigned integer representation of a negative integerprintf("%%f = '%f ' \ n",$n);//floating point representationprintf("%%o = '%o ' \ n",$n);//octal representationprintf("%%s = '%s ' \ n",$n);//string Representationprintf("%%x = '%x ' \ n",$n);//hexadecimal representation (lower-case)printf("%%x = '%x ' \ n",$n);//hexadecimal representation (upper-case)printf("%%+d = '%+d ' \ n",$n);//Sign specifier on a positive integerprintf("%%+d = '%+d ' \ n",$u);//Sign specifier on a negative integer%b = ' 10100111101010011010101101 '%c = ' A '%d = ' 43951789 '%e = ' 4.39518e+7 '%u = ' 43951789 '%u = ' 4251015507 '%f = ' 43951789.00 0000 '%o = ' 247523255 '%s = ' 43951789 '%x = ' 29ea6ad '%x = ' 29ea6ad '%+d = ' +43951789 '%+d = '-43951789 '
View Code
specifier |
Output |
Example |
D |
Signed decimal integer |
392 |
U |
Unsigned decimal Integer |
7235 |
O |
Unsigned octal |
610 |
X |
Unsigned hexadecimal integer |
7fa |
X |
Unsigned hexadecimal integer (uppercase) |
7FA |
F |
Decimal floating point, lowercase |
392.65 |
F |
Decimal floating point, uppercase |
392.65 |
E |
Scientific notation (mantissa/exponent), lowercase |
3.9265e+2 |
E |
Scientific notation (mantissa/exponent), uppercase |
3.9265E+2 |
G |
Use the shortest representation: %e or %f |
392.65 |
G |
Use the shortest representation: %E or %F |
392.65 |
C |
The argument is treated as a integer, and presented as the character with that ASCII value |
A |
S |
String of characters |
Sample |
P |
The argument is treated as an integer, and presented as a binary number |
10100011 |
% |
A % followed by another % character would write a single percent to the stream. |
% |
Indicate:
This article mainly refers to the official document: Http://php.net/manual/en/function.sprintf.php. A summary of the documentation and corrections to the parts of the document that are biased. The different places are as follows:
1. The flags can be unordered. Not as the document says-it needs to be in a fixed order. The example in the flags can illustrate the problem. (It should also be noted that the padding 0 and the left alignment flag '-' are combined to produce unexpected results, see the example below)
echo sprintf ("|%-04d|\n", -2);
//output: |-2 |
Summary of formatting rules in PHP sprintf, printf and other strings formatted output