标签: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
其中:
<?php $num = 5; $location = ‘tree‘; $format = ‘The %2$s contains %1$d monkeys‘; echo sprintf($format, $num, $location);
flag | 描述 |
+ | 默认情况下,只有在数字为负数时,才会显示出符号位‘-’。如果数字为正数,则不显示符号位‘+’。本flag设置之后,不论数字为正或为负,都显示符号位。 |
‘sign 或者 0 |
本flag用来设置用来填充的符号。填充符号是为了是输出的字符串达到width指定的长度。默认的填充符是空格。标准的指定填充符的方式是:单引号+填充符,不过对于0作为填充符,即 可以使用标准定义,也可以直接申明。 |
- | 本flag用来指明输出的结果是左对齐还是右对齐。缺省下是右对齐,申明本flag后,为左对齐。 |
<?php echo 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::| */
<?php $money = 123.1234; echo sprintf("%.2f", $money); //123.1
<?php $n = 43951789; $u = -43951789; $c = 65; // ASCII 65 is ‘A‘ // notice the double %%, this prints a literal ‘%‘ character printf("%%b = ‘%b‘\n", $n); // binary representation printf("%%c = ‘%c‘\n", $c); // print the ascii character, same as chr() function printf("%%d = ‘%d‘\n", $n); // standard integer representation printf("%%e = ‘%e‘\n", $n); // scientific notation printf("%%u = ‘%u‘\n", $n); // unsigned integer representation of a positive integer printf("%%u = ‘%u‘\n", $u); // unsigned integer representation of a negative integer printf("%%f = ‘%f‘\n", $n); // floating point representation printf("%%o = ‘%o‘\n", $n); // octal representation printf("%%s = ‘%s‘\n", $n); // string representation printf("%%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 integer printf("%%+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‘
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. | % |
echo sprintf ("|%-04d|\n", -2);
//输出: |-2 |
PHP中sprintf、printf等字符串格式化输出中的格式规则总结
标签:text use mda key put 多少 ecif rac open
原文地址:http://www.cnblogs.com/jade640/p/7100656.html