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后,為左對齊。

    <?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::|
    */
  • 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 '%' 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'
    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  |

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM