PHP sprintf function

 The PHP sprintf function can insert formatted character elements into a specific position in a string variable . The processing method for inserting is the same as that of the printf function, but the sprintf function only performs the insertion work, and is not responsible for the actions of output on the web page. That is to say, after the sprintf function is processed, it will only return a new string variable for subsequent other processing. If it is to be output to a web page, it must be done through echo or print . If no other processing is needed, just output the converted string variable directly, and you can use the printf function .


PHP sprintf basic syntax

string sprintf (string $format, $args1, $args2, $args3, $arg4, $arg5 ...)


The first parameter in the parentheses of the sprintf function, $format, is the original formatting conversion string , and the following series of $args are the character parameters to be inserted into the $format string , and each character parameter will be sequentially inserted into the original format Convert the percent sign (%) in the string ($format) and complete the formatting. For example, $args1 is inserted into the first% position of $format, $args2 is inserted into the second% position, and $args3 is inserted To the 3rd% position... and so on, there are detailed references in the example.

The $format parameter in the original string is usually written as% plus the delimitation code. The possible forms are as follows
%%Returns the percentage sign, no parameters are required
%bBinary integer parameter
%cThe parameter is treated as an ASCII value character integer
%dDecimal integer parameter
%eParameters will be treated as scientific notation, such as 1.2e+2
%ESimilar to %e, but presented in uppercase English letters, such as 1.2E+2
%uThe parameter is treated as an unsigned decimal integer
%fFloating point number (locale aware)
%FFloating point numbers (non-locale aware, new in PHP 4.3.10 and PHP 5.0.3)
%gShorter than %e and %f.
%GShorter than %E and %F.
%oOctal integer parameter
%sString
%xHexadecimal (lowercase letters)
%XHexadecimal (uppercase letters)

This table can also be compared to the $format parameter used in the PHP printf function .

PHP sprintf reference example 1: Bring in string, number and floating point number conversion
<?php
$string ='to';
$number = '5';
$format ='Welcome %s Wibibi.%f is float of %u.';
echo sprintf($format,$string,$number,$number );
?>
The output of the above example
Welcome to Wibibi.5.000000 is float of 5.
In the example, we use three parameters, let sprintf try to convert, the value of the first parameter $string is the string "to", which is easily brought to the position of the first% symbol in the $format variable , and the second parameter The original value of $number is the number 5. In the second% symbol of $format, it is formatted as a floating point number (%f), so the output result has a decimal point and reaches five digits after the decimal point, because we have not set it specifically The number of decimal places. The next example will control the number of decimal places. The third parameter is formatted as a decimal integer, so it is directly 5.

PHP sprintf reference example two, control the number of decimal places
<?php
$number = '5';
$format = "%1\$.3f is float of %u.";
echo sprintf($format,$number,$number);
?>
Example output
5.000 is float of 5.
In the $format parameter of sprintf, the first formatting part written as "%1\$.3f" means that the floating-point number is processed to the third digit after the decimal point, and the number represents the number of decimal places. The right side of the equal sign of $format must use double quotation marks ("") to package the content. If single quotation marks ('') are used, the sprintf function cannot successfully import variables and format them.

Post a Comment

0 Comments