PHP Tutorial - PHP vsprintf() Function






Definition

The vsprintf() function writes a formatted string to a variable.

Unlike sprintf(), the arguments in vsprintf(), are placed in an array. The array elements will be inserted at the percent (%) signs in the main string.

Syntax

PHP vsprintf() Function has the following syntax

vsprintf(format,argarray)

Parameter

This function takes a variable number of parameters: the first parameter is a format string, followed by zero or other parameters of various types.

The format strings for sprintf() is listed as follows.

FormatMeaning
%% A literal percent character; no matching parameter is required
%b Parameter is an integer; output it as binary
%c Parameter is an integer; output it as a character with that ASCII value
%d Parameter is a positive integer; output it as decimal
%e Scientific notation using a lowercase (e.g. 1.2e+2)
%E Scientific notation using a uppercase (e.g. 1.2E+2)
%f Parameter is a float; express it as a float
%F Floating-point number (not local settings aware)
%g shorter of %e and %f
%G shorter of %E and %f
%o Parameter is an integer; output it as octal
%s Parameter is a string; output it as a string
%uUnsigned decimal number (equal to or greather than zero)
%x Parameter is an integer; output it as hexadecimal with lowercase letters
%X Parameter is an integer; output it as hexadecimal with uppercase letters




Additional format

Additional format values are placed between the % and the letter (example %.2f):

Additional formatMeaning
+Forces both + and - in front of numbers. By default, only negative numbers are marked
'What to use as padding. Default is space. Must be used together with the width specifier. Example: %'q20s (this uses "q" as padding)
-Left-justifies the variable value
[0-9]Minimum width held of to the variable value
.[0-9]Number of decimal digits or maximum string length

The multiple additional format values must be in the same order as above.





Return

PHP vsprintf() Function returns array values as a formatted string.

fprintf(), vfprintf(), printf(), sprintf() and vprintf().

Example 1

Write a formatted string to a variable:


<?php/*from w w w  . j av a  2  s . c o  m*/
$number = 2;
$str = "PHP";
$txt = vsprintf("There are %u million developers using %s.",array($number,$str));
echo $txt."\n";

$str1 = "Hello";
$str2 = "Hello world!";

echo vsprintf("[%s]",array($str1))."\n";
echo vsprintf("[%8s]",array($str1))."\n";
echo vsprintf("[%-8s]",array($str1))."\n";
echo vsprintf("[%08s]",array($str1))."\n"; 
echo vsprintf("[%'*8s]",array($str1))."\n";
echo vsprintf("[%8.8s]",array($str2))."\n"; 
?>

The code above generates the following result.

Example 2

Using the format value %f:


<?php
$num1 = 123;
$num2 = 456;
$txt = vsprintf("%f%f",array($num1,$num2));
echo $txt;
?>

The code above generates the following result.

Example 3

Use of placeholders:


<?php
$number = 123;
$txt = vsprintf("With 2 decimals: %1\$.2f
\nWith no decimals: %1\$u",array($number));
echo $txt;
?>

The code above generates the following result.