sprintf

(PHP 3, PHP 4, PHP 5)

sprintf -- フォーマットされた文字列を返す

説明

string sprintf ( string format [, mixed args [, mixed ...]] )

フォーマット文字列formatに基づき生成された 文字列を返します。

フォーマット文字列は0個以上のディレクティブ(指示子)により 構成されます。ディレクティブには、そのまま結果にコピーされる (%を除く)通常の文字と変換指定子 (conversion specifications)があり、取り出される際は どちらもそれ自身がパラメータとなります。このことは sprintf()の場合だけでなくprintf() の場合も同様です。

各変換指定子はパーセント記号(%)の後に、これら の要素が一つ以上続いたものになります。

  1. An optional sign specifier that forces a sign (- or +) to be used on a number. By default, only the - sign is used on a number if it's negative. This specifier forces positive numbers to have the + sign attached as well, and was added in PHP 4.3.0.

    オプションのパディング指定子。これは、 文字列が正しい長さになるまでどんな文字で埋めるかということを 指定します。これは空白かまたは0(文字'0') のいずれかです。デフォルトでは空白で埋められます。 これ以外のパディング文字を指定するには、その文字の前に単一 引用符(')を置きます。後述の例を参照して ください。

  2. オプションのアラインメント指定子。これは 結果を左寄せまたは右寄せにしたい場合に指定します。デフォルトは 右寄せです。ここで-文字を指定すると左寄せ となります。

  3. オプションの数字。これは表示幅指定子です。 結果を(最低)何桁にするかを指定します。

  4. オプションの精度指定子。これは、浮動小数点 数に対して何個の数字を表示するかを指定します。 When using this specifier on a string, it acts as a cutoff point, setting a maximum character limit to the string.

  5. 型指定子。引数を何の型として扱うかを指定 します。指定できる型を以下に示します。

    % - パーセント文字。引数は不要です。
    b - 引数を整数として扱い、バイナリの数値 として表現します。
    c - 引数を整数として扱い、その ASCII 値 の文字として表現します。
    d - 引数を整数として扱い、10 進数として 表現します。
    e - the argument is treated as scientific notation (e.g. 1.2e+2).
    u - 引数を整数として扱い、符号無しの10進 数として表現します。
    f - 引数を double として扱い、浮動小数点数 として表現します。
    F - the argument is treated as a float, and presented as a floating-point number (non-locale aware). Available since PHP 4.3.10 and PHP 5.0.3.
    o - 引数を整数として扱い、8 進数として 表現します。
    s - 引数を文字列として扱い、表現します。
    x - 引数を整数として扱い、16 進数として (小文字で)表現します。
    X - 引数を整数として扱い、16 進数として (大文字で)表現します。

PHP4.0.6以降でフォーマット文字列で引数の番号付け/交換が サポートされました。以下に例を示します。

例 1. 引数の交換

<?php
$format
= "There are %d monkeys in the %s";
printf($format,$num,$location);
?>
この出力は、"There are 5 monkeys in the tree" のようになります。 ここで、フォーマット文字列が別のファイルにある場合を考えてみましょ う。これは、出力を国際化したりする場合に行われる可能性があります。 この場合、次の書き変えられます。

例 2. 引数の交換

<?php
$format
= "The %s contains %d monkeys";
printf($format, $num, $location);
?>
ここで、問題が発生します。フォーマット文字列における置換指示子の 順番は、コードにおける引数の順番と一致していません。コードは変更 せず、置換指示子が参照するフォーマット文字列で指示を行う方が望ま しいと言えます。フォーマット文字列を次のように書き換えてみましょ う。

例 3. 引数の交換

<?php
$format
= "The %2\$s contains %1\$d monkeys";
printf($format, $num, $location);
?>
加えて、これによりコードに引数を追加せずに置換指示子を複数回使用 することも可能になります。例えば、次のようになります。

例 4. 引数の交換

<?php
$format
= "The %2\$s contains %1\$d monkeys.  
           That's a nice %2\$s full of %1\$d monkeys."
;
printf($format, $num, $location);
?>

printf(), sscanf(), fscanf(), vsprintf(), number_format() も参照ください。

例 5. printf(): various examples

<?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
?>

The printout of this program would be:

%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'

例 6. printf(): string specifiers

<?php
$s
= 'monkey';
$t = 'many monkeys';

printf("[%s]\n",      $s); // standard string output
printf("[%10s]\n",    $s); // right-justification with spaces
printf("[%-10s]\n",   $s); // left-justification with spaces
printf("[%010s]\n",   $s); // zero-padding works on strings too
printf("[%'#10s]\n",  $s); // use the custom padding character '#'
printf("[%10.10s]\n", $t); // left-justification but with a cutoff of 10 characters
?>

The printout of this program would be:

[monkey]
[    monkey]
[monkey    ]
[0000monkey]
[####monkey]
[many monke]

例 7. sprintf: 整数を0でパディング

<?php
$isodate
= sprintf("%04d-%02d-%02d", $year, $month, $day);
?>

例 8. sprintf: 通貨をフォーマットする例

<?php
$money1
= 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money は "123.1" を出力します。
$formatted = sprintf ("%01.2f", $money);
// echo $formatted は "123.10"を出力します
?>

例 9. sprintf(): scientific notation

<?php
$number
= 362525200;

echo
sprintf("%.3e", $number); // outputs 3.63e+8
?>