(PHP 4, PHP 5)
sprintf — Return a formatted string
Returns a string produced according to the formatting string format.
The format string is composed of zero or more directives: ordinary characters (excluding %) that are copied directly to the result, and conversion specifications, each of which results in fetching its own parameter. This applies to both sprintf() and printf().
Each conversion specification consists of a percent sign (%), followed by one or more of these elements, in order:
A type specifier that says what type the argument data should be treated as. Possible types:
The format string supports argument numbering/swapping. Here is an
example:
Example #1 Argument swapping Example #2 Argument swapping Example #3 Argument swapping Example #4 Argument swapping Example #5 Position specifier with other specifiers 以上例程会输出:
<?php
This will output "There are 5 monkeys in the tree". But
imagine we are creating a format string in a separate file,
commonly because we would like to internationalize it and we
rewrite it as:
$num = 5;
$location = 'tree';
$format = 'There are %d monkeys in the %s';
printf($format, $num, $location);
?>
<?php
We now have a problem. The order of the placeholders in the
format string does not match the order of the arguments in the
code. We would like to leave the code as is and simply indicate
in the format string which arguments the placeholders refer to.
We would write the format string like this instead:
$format = 'The %s contains %d monkeys';
printf($format, $num, $location);
?>
<?php
An added benefit here is that you can repeat the placeholders without
adding more arguments in the code. For example:
$format = 'The %2$s contains %1$d monkeys';
printf($format, $num, $location);
?>
<?php
When using argument swapping, the n$
position specifier must come immediately
after the percent sign (%), before any other
specifiers, as shown in the example below.
$format = 'The %2$s contains %1$d monkeys.
That's a nice %2$s full of %1$d monkeys.';
printf($format, $num, $location);
?>
<?php
$format= 'The %2$s contains %1$04d monkeys';
printf($format, $num, $location);
?>
The tree contains 0005 monkeys
Returns a string produced according to the formatting string format.
版本 | 说明 |
---|---|
4.0.6 | Support for argument numbering/swapping was added |
Example #6 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); // binary representation
printf("%%c = '%c'
", $c); // print the ascii character, same as chr() function
printf("%%d = '%d'
", $n); // standard integer representation
printf("%%e = '%e'
", $n); // scientific notation
printf("%%u = '%u'
", $n); // unsigned integer representation of a positive integer
printf("%%u = '%u'
", $u); // unsigned integer representation of a negative integer
printf("%%f = '%f'
", $n); // floating point representation
printf("%%o = '%o'
", $n); // octal representation
printf("%%s = '%s'
", $n); // string representation
printf("%%x = '%x'
", $n); // hexadecimal representation (lower-case)
printf("%%X = '%X'
", $n); // hexadecimal representation (upper-case)
printf("%%+d = '%+d'
", $n); // sign specifier on a positive integer
printf("%%+d = '%+d'
", $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'
Example #7 printf(): string specifiers
<?php
$s = 'monkey';
$t = 'many monkeys';
printf("[%s]
", $s); // standard string output
printf("[%10s]
", $s); // right-justification with spaces
printf("[%-10s]
", $s); // left-justification with spaces
printf("[%010s]
", $s); // zero-padding works on strings too
printf("[%'#10s]
", $s); // use the custom padding character '#'
printf("[%10.10s]
", $t); // left-justification but with a cutoff of 10 characters
?>
以上例程会输出:
[monkey] [ monkey] [monkey ] [0000monkey] [####monkey] [many monke]
Example #8 sprintf(): zero-padded integers
<?php
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);
?>
Example #9 sprintf(): formatting currency
<?php
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"
?>
Example #10 sprintf(): scientific notation
<?php
$number = 362525200;
echo sprintf("%.3e", $number); // outputs 3.625e+8
?>