PHP vsprintf() Function
Example
Write the formatted string into a variable:
<?php $number = 9; $str = "Beijing"; $txt = vsprintf("There are 쥕lion bicycles in %s.", array($number, $str)); echo $txt; ?>
Definition and Usage
The vsprintf() function writes the formatted string into a variable.
Unlike sprintf(), the parameters in vsprintf() are located in an array. Array elements will be inserted into the main string at the percentage (%) symbol. This function is executed step by step. At the first % symbol, the first array element is inserted, at the second % symbol, the second array element is inserted, and so on.
Note:If there are more % symbols than arg If there are parameters, you must use placeholders. Placeholders are inserted after the % symbol, consisting of a number and '\$'. See example 2.
Tip:Related functions:
Syntax
vsprintf(format,argarray)
Parameters | Description |
---|---|
format |
Required. Specifies the string and how to format the variables within it. Possible format values:
Additional format values. They must be placed between % and the letter (for example %.2f):
Note:If multiple format values are used, they must be used in the order specified above and cannot be mixed. |
argarray | Required. An array with parameters that will be inserted format The % symbol in the string. |
Technical details
Return value: | Returns the array values in the form of a formatted string. |
PHP version: | 4.1.0+ |
More examples
Example 1
Use the format value %f:
<?php $num1 = 123; $num2 = 456; $txt = vsprintf("%f%f",array($num1,$num2)); echo $txt; ?>
Example 2
Using placeholders:
<?php $number = 123; $txt = vsprintf("With two decimal places:%1$.2f<br>No decimal:%1$u",array($number)); echo $txt; ?>
Example 3
Use sprintf() to demonstrate all possible format values:
<?php $num1 = 123456789; $num2 = -123456789; $char = 50; // ASCII character 50 is 2 // Comment: The format value "%%" returns the percent sign echo sprintf("%%b = %b",$num1)."<br>"; // Binary number echo sprintf("%%c = %c",$char)."<br>"; // ASCII character echo sprintf("%%d = %d",$num1)."<br>"; // Signed decimal number echo sprintf("%%d = %d",$num2)."<br>"; // Signed decimal number echo sprintf("%%e = %e",$num1)."<br>"; // Scientific notation (lowercase) echo sprintf("%%E = %E",$num1)."<br>"; // Scientific notation (uppercase) echo sprintf("%鑾u",$num1)."<br>"; // Unsigned decimal number (positive) echo sprintf("%鑾u",$num2)."<br>"; // Unsigned decimal number (negative) echo sprintf("%%f = %f",$num1)."<br>"; // Floating-point number (considering local settings) echo sprintf("%%F = %F",$num1)."<br>"; // Floating-point number (not considering local settings) echo sprintf("%%g = %g",$num1)."<br>"; // Shorter than %e and %f echo sprintf("%%G = %G",$num1)."<br>"; // Shorter than %E and %f echo sprintf("%%o = %o",$num1)."<br>"; // Octal number echo sprintf("%%s = %s",$num1)."<br>"; // String echo sprintf("%%x = %x",$num1)."<br>"; // Hexadecimal Number (Lowercase) echo sprintf("%%X = %X",$num1)."<br>"; // Hexadecimal Number (Uppercase) echo sprintf("%%+d = %+d",$num1)."<br>"; // Positive Sign specifier echo sprintf("%%+d = %+d",$num2)."<br>"; // Negative Sign specifier ?>
Example 4
Demonstration of String Literals:
<?php $str1 = "Hello"; $str2 = "Hello world!"; echo vsprintf("[%s]",array($str1))."<br>"; echo vsprintf("[%8s]",array($str1))."<br>"; echo vsprintf("[%-8s]",array($str1))."<br>"; echo vsprintf("[%08s]",array($str1))."<br>"; echo vsprintf("[%'*8s]",array($str1))."<br>"; echo vsprintf("[%8.8s]",array($str2))."<br>"; ?>