PHP vsprintf() Function

In this chapter you will learn:

  1. Definition for PHP vsprintf() Function
  2. Syntax for PHP vsprintf() Function
  3. Parameter for PHP vsprintf() Function
  4. Additional format for PHP printf() Function
  5. Return for PHP vsprintf() Function
  6. Related functions
  7. Example - Write a formatted string to a variable
  8. Example - Using the format value %f
  9. Example - Use of placeholders

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 j  a  v  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/* ja v  a  2  s  .  c om*/
$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/*from   ja  va2s.  c o  m*/
$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.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP wordwrap() Function
  2. Syntax for PHP wordwrap() Function
  3. Parameter for PHP wordwrap() Function
  4. Return for PHP wordwrap() Function
  5. Example - wrap long string
Home » PHP Tutorial » PHP String Functions
PHP addcslashes() Function
PHP addslashes() Function
PHP bin2hex() Function
PHP chop() Function
PHP chr() Function
PHP chunk_split() Function
PHP convert_cyr_string() Function
PHP convert_uudecode() Function
PHP convert_uuencode() Function
PHP count_chars() Function
PHP crc32() Function
PHP crypt() Function
PHP echo() Function
PHP get_html_translation_table() Function
PHP hebrev() Function
PHP hebrevc() Function
PHP hex2bin() Function
PHP html_entities() Function
PHP html_entity_decode() Function
PHP htmlspecialchars() Function
PHP htmlspecialchars_decode() Function
PHP join() Function
PHP lcfirst() Function
PHP levenshtein() Function
PHP localeconv() Function
PHP ltrim() Function
PHP md5() Function
PHP metaphone() Function
PHP money_format() Function
PHP nl2br() Function
PHP nl_langinfo() Function
PHP number_format() Function
PHP ord() Function
PHP parse_str() Function
PHP print() Function
PHP print_r() Function
PHP printf() Function
PHP quoted_printable_decode() Function
PHP quoted_printable_encode() Function
PHP quotemeta() Function
PHP rawurldecode() Function
PHP rawurlencode() Function
PHP rtrim() Function
PHP setlocale() Function
PHP sha1() Function
PHP similar_text() Function
PHP soundex() Function
PHP sprintf() Function
PHP sscanf() Function
PHP str_getcsv() Function
PHP str_ireplace() Function
PHP str_pad() Function
PHP str_repeat() Function
PHP str_replace() Function
PHP str_rot13() Function
PHP str_shuffle() Function
PHP str_split() Function
PHP str_word_count() Function
PHP strcasecmp() Function
PHP strchr() Function
PHP strcmp() Function
PHP strcoll() Function
PHP strcspn() Function
PHP strip_tags() Function
PHP stripcslashes() Function
PHP stripslashes() Function
PHP stristr() Function
PHP strlen() Function
PHP strnatcasecmp() Function
PHP strnatcmp() Function
PHP strncasecmp() Function
PHP strncmp() Function
PHP strpbrk() Function
PHP strpos() Function
PHP strrchr() Function
PHP strrev() Function
PHP strripos() Function
PHP strrpos() Function
PHP strspn() Function
PHP strstr() Function
PHP strtok() Function
PHP strtolower() Function
PHP strtoupper() Function
PHP strtr() Function
PHP substr() Function
PHP substr_compare() Function
PHP substr_count() Function
PHP substr_replace() Function
PHP trim() Function
PHP ucfirst() Function
PHP ucwords() Function
PHP utf8_decode() Function
PHP utf8_encode() Function
PHP vprintf() Function
PHP vsprintf() Function
PHP wordwrap() Function