首页 > 代码库 > PHP中sprintf、printf等字符串格式化输出中的格式规则总结

PHP中sprintf、printf等字符串格式化输出中的格式规则总结

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


    specifierOutputExample
    d Signed decimal integer392
    uUnsigned decimal integer7235
    oUnsigned octal610
    xUnsigned hexadecimal integer7fa
    XUnsigned hexadecimal integer (uppercase)7FA
    fDecimal floating point, lowercase392.65
    FDecimal floating point, uppercase392.65
    eScientific notation (mantissa/exponent), lowercase3.9265e+2
    EScientific notation (mantissa/exponent), uppercase3.9265E+2
    gUse the shortest representation: %e or %f392.65
    GUse the shortest representation: %E or %F392.65
    cthe argument is treated as an integer, and presented as the character with that ASCII valuea
    sString of characterssample
    pthe argument is treated as an integer, and presented as a binary number10100011
    %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等字符串格式化输出中的格式规则总结