Copy Code code as follows:
<?php
sprintf () function that returns the formatted string for the value
String sprintf (String $format [, Mixed $args [, mixed $ ...]])
$y = 11;
$m = 3;
$d = 9;
$date = Sprinf ('%04d-%02d-%02d ', $y, $m, $d);
Echo $date; 0011-0
printf () function, which returns the length of the formatted string
int printf (string $format [, Mixed $args [, mixed $ ...]]
$num = 3.14;
printf ("character padding% #6.2s", $num); # #3.14
The character length is 6, there are several points after 2, less than 6 bits, #填充
sprintf () differs from printf ()
Syntax format, except that the return value is different
Definitions and usage
The sprintf () function writes the formatted string to a variable.
Grammar
sprintf (format,arg1,arg2,arg++)
Parameters |
Description |
Format |
Necessary. Conversion format. |
Arg1 |
Necessary. Specify the parameter to be inserted into the first% symbol in the format string. |
Arg2 |
Optional. Specify the parameter to be inserted into the second% symbol in the format string. |
arg++ |
Optional. Specify the parameters to be inserted into the format string at the third to fourth and so% symbols. |
Description
The parameter format is the format of the transformation, starting with the percent sign ("%") to the end of the converted character. The following possible format values:
- %%-return percent symbol
- %b-Binary number
- %c-Characters with ASCII values
- %d-Signed decimal digits
- %e-Continuous counting method (e.g. 1.5e+3)
- %u-Unsigned decimal digits
- %f-floating-point number (Local settings Aware)
- %F-floating-point number (not local settings aware)
- %o-Eight binary number
- %s-String
- %x-16 Number (small letter)
- %x-16 Number (capital letter)
Arg1, arg2, + + etc parameters will be inserted into the main string of the percent semicolon (%) Symbol Place. The function is executed incrementally. In the first% symbol, insert arg1, at the second% symbol, insert arg2, and so on.
Example
Example 1
Copy Code code as follows:
<?php
$str = "Hello";
$number = 123;
$txt = sprintf ("%s World"). Day number%u ", $STR, $number);
Echo $txt;
?>
Output:
Hello World. Day Number 123
Example 2
Copy Code code as follows:
<?php
$number = 123;
$txt = sprintf ("%f", $number);
Echo $txt;
?>
Output:
123.000000
Example 3
Copy Code code as follows:
<?php
$number = 123;
$txt = sprintf ("With 2 decimals:%1\$.2f<br/>with no decimals:%1\ $u", $number);
Echo $txt;
?>
Output:
With 2 decimals:123.00
With no decimals:123
You can refer to http://www.jb51.net/w3school/php/func_string_sprintf.htm in more detail