標籤:text use mda key put 多少 ecif rac open
sprintf、printf輸出格式化字串。
比如sprintf()的函數原型如下:
string sprintf ( string $format
[, mixed $args
[, mixed $...
]] )
其中$format用於指定輸出的字串的格式。
進過總結$format遵守以下原型:
%[n$][flags][width][.precision]specifier
其中:
- n$ 是position specifier,指明本預留位置代表的是哪個參數
<?php$num = 5;$location = ‘tree‘;$format = ‘The %2$s contains %1$d monkeys‘;echo sprintf($format, $num, $location);
- flags 是一些標誌,用來表明是否顯示+號、填充字元、對齊。具體flags見下表
flag |
描述 |
+ |
預設情況下,只有在數字為負數時,才會顯示出符號位‘-’。如果數字為正數,則不顯示符號位‘+’。本flag設定之後,不論數字為正或為負,都顯示符號位。 |
‘sign 或者 0 |
本flag用來設定用來填充的符號。填充符號是為了是輸出的字串達到width指定的長度。預設的填充符是空格。標準的指定填充符的方式是:單引號+填充符,不過對於0作為填充符,即 可以使用標準定義,也可以直接申明。 |
- |
本flag用來指明輸出的結果是靠左對齊還是靠右對齊。預設下是靠右對齊,申明本flag後,為靠左對齊。 |
<?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 ("|%04d|\n", -2);echo sprintf ("|%‘:4d|\n", -2);echo sprintf ("|%-‘:4d|\n", -2);/*outputs:|-002||::-2||-2::|*/
- width 指明本格式輸出至少有多少字元。即指明字元的輸出長度。見上例。
- .precision 指明對於浮點數,應該保留幾位小數
<?php$money = 123.1234;echo sprintf("%.2f", $money); //123.1
- specifier specifier指明應該將參數以何種參數類型對待。
<?php$n = 43951789;$u = -43951789;$c = 65; // ASCII 65 is ‘A‘// notice the double %%, 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.000000‘%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 an 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 will write a single % to the stream. |
% |
註明:
本文主要參考官方文檔:http://php.net/manual/en/function.sprintf.php 。並對文檔進行了總結,以及對文檔部分有偏差部分的糾正。不同處如下:
1. flags可以是無序的。並不像文檔所說——需要按照固定的順序。flags處的例子可以說明問題。(另外需要指出的是:填充符0和靠左對齊標誌‘-’合用會產生意想不到的結果,見下面的例子)
echo sprintf ("|%-04d|\n", -2);
//輸出: |-2 |
PHP中sprintf、printf等字串格式化輸出中的格式規則總結