vsprintfRETURNS a formatted STRING.
This function is
NOT SELF-SUFFICIENT because it requires the use of auxiliary functions to display results.
Undue substitutions will produce unpredictable results.
<?php
int vsprintf ( str $format , arr $args )
where,
$format = STRING FORMAT, with internal,
% replaceable specifiers
( SEE the below TABLE )
$args = SUBSTITUTE ARGUMENTS
?>
$formatOperates as
sprintf but accepts an
ARRAY of arguments, rather than a variable number of arguments.
As
sprintf, each
SPECIFIER, present in
FORMATTED STRING, must have its own
SUBSTITUTE ARGUMENT.
SPECIFIER % |
TYPE |
ARGUMENT |
COMMENT |
%% |
|
literally % |
|
%b |
INTEGER |
Represented as BINARY |
|
%c |
INTEGER |
Represented as CHARACTER - ASCII |
|
%d |
INTEGER |
Represented as DECIMAL NUMBER |
+/- |
%e |
FLOAT |
SCIENTIFIC NOTATION |
|
%E |
FLOAT |
As %e with the use of UPPERCASE
|
|
%u |
INTEGER |
Presented as an UNSIGNED DECIMAL number. |
no signal |
%f |
FLOAT |
Compatible with LOCALE |
|
%F |
FLOAT |
As %f not compatible with LOCALE |
|
%g |
FLOAT |
Shorter of %e and %f. |
|
%G |
FLOAT |
Shorter of %E e %F |
|
%o |
INTEGER |
Presented as an OCTAL number |
|
%s |
STRING |
STRING
|
|
%x |
INTEGER |
Presented as an HEXADECIMAL number |
lowercase |
%X |
INTEGER |
Presented as an HEXADECIMAL number |
UPERCASE |
ADDITIONAL FORMATS |
0 |
ZERO |
FILLING WITH ZERO |
Number of desired decimal places |
" " |
SPACE |
FILLING WITH SPACES |
Number of desired places |
- |
HYPHEN |
Left-aligned value |
Right-aligned as default |
N1.N2 |
N1 |
INTEGER
|
NUMBER OF DIGITS |
N2 |
DECIMAL |
NUMBER OF DIGITS |
ed48 |
$args
Substitute arguments.
EXERCISE
<?php
$strA = 'If everyone liked %s, what would be the %s?';
$arg1 = ['BLUE', 'YELLOW'];
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This function is AUTO SUFFICIENT
The displayed data is made directly
without the need for ancillary functions
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
echo vsprintf($strA, $arg1) . '<br><br>';
?>
RESULT
If everyone liked BLUE , what would be the YELLOW ?
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ATTENTION FOR THE NON COMPATIBILITY WITH LOCALE
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
setlocale(LC_ALL, "pt-br", "pt_BR.utf-8");
$cus_pt = "This product costs %s%1.2F per unit.";
$curval_br = [ 'R$', 5.71 ];
echo vsprintf($cus_pt, $curval_br) . '<br><br>';
setlocale(LC_ALL, "en-us", "en_US");
$cus_us = "This product costs %s%1.2f per unit.";
$curval_us = [ 'U$', 1.00 ];
echo vsprintf($cus_us, $curval_us);
?>
RESULT
This product costs R$5.71 per unit.
This product costs U$1.00 per unit.
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ATTENTION FOR THE COMPATIBILITY WITH LOCALE
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
setlocale(LC_ALL, "de-DE", "de_DE");
$cus_pt = "Dieses Produkt kostet %s%1.2f pro Einheit.";
$curval_br = [ '€', 1.00 ];
echo vsprintf($cus_pt, $curval_br);
echo '<br><br>';
setlocale(LC_ALL, "en_US", "en-US");
$cus_us = "This product costs %s%1.2f per unit.";
$curval_us = [ 'US$', 1.17 ];
echo vsprintf($cus_us, $curval_us);
?>
RESULT
Dieses Produkt kostet €1,00 pro Einheit.
This product costs US$1.13 per unit.
EXERCISE
<?php
setlocale(LC_ALL, "en-us", "en_US", "american", "american english", "american-english", "english-american", "english-us", "english-usa", "enu", "us", "usa");
$str04n = <<<EOD
In PHP there are TWO types
of variables:
INTERNAL: %s
and OF USER: %s
EOD;
$arg4 = [ '(predefined) ', '(user-defined).'];
$res04n = vsprintf($str04n, $arg4);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This function is AUTO SUFFICIENT
The displayed data is made directly
without the need for ancillary functions,
however, if you treat it as a VARIABLE and
use: echo or print to display it,
this will additionally show the
number of characters involved
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
echo $res04n . '<br><br>';
?>
RESULT
In PHP there are TWO types of variables: INTERNAL: (predefined) and OF USER: (user-defined).
EXERCISE
<?php
setlocale(LC_ALL, "en-us", "en_US", "american",
"american english", "american-english",
"english-american", "english-us",
"english-usa", "enu", "us", "usa");
$str05 = '2018.99987 + 421.9876 = %f';
$arg05 = [2018.99987 + 421.9876 ];
$res05 = vsprintf($str05, $arg05);
echo '<br><br>' . $res05 . '<br><br>';
setlocale(LC_ALL, "pt-br.utf-8",
"pt_BR.utf-8", "portuguese-brazil", "ptb");
$res05 = vsprintf($str05, $arg05);
echo $res05 . '<br><br>';
?>
RESULT
2018.99987 + 421.9876 = 2440.987470
2018.99987 + 421.9876 = 2440,987470
EXERCISE
<?php
$year06 = idate('Y');
// current year
$str06 = "$year06<br>is equal to<br>
%o (octal)<br>and to<br>
%X (hexadecimal)";
$arg06 = [ $year06, $year06 ];
$res06 = vsprintf($str06, $arg06);
echo $res06 . '<br><br>';
?>
EXERCISE
<?php
echo "Testing vsprintf() :<br>
basic functionality - using string format.<br>";
// Initialise all required variables
$format = "format";
$format1 = "%s";
$format2 = "%s %s";
$format3 = "%s %s %s";
$arg1 = array("one");
$arg2 = array("one","two");
$arg3 = array("one","two","three");
echo '<br><br>';
var_dump( vsprintf($format1,$arg1) );
echo '<br><br>';
var_dump( vsprintf($format2,$arg2) );
echo '<br><br>';
var_dump( vsprintf($format3,$arg3) );
?>
EXERCISE
<?php
echo "Testing vsprintf() :<br>
basic functionality - using integer format.<br>";
// Initialise all required variables
$format = "format";
$format1 = "%d";
$format2 = "%d %d";
$format3 = "%d %d %d";
$arg1 = array(111);
$arg2 = array(111,222);
$arg3 = array(111,222,333);
echo '<br><br>';
var_dump( vsprintf($format1,$arg1) );
echo '<br><br>';
var_dump( vsprintf($format2,$arg2) );
echo '<br><br>';
var_dump( vsprintf($format3,$arg3) );
?>
EXERCISE
<?php
echo "Testing vsprintf() :<br>
basic functionality - using float format.<br>";
// setlocale(LC_ALL, "es-ES", "es_ES");
// Initialise all required variables
$format = "format";
$format1 = "%f";
$format2 = "%f %f";
$format3 = "%f %f %f";
$format11 = "%F";
$format22 = "%F %F";
$format33 = "%F %F %F";
$arg1 = array(11.11);
$arg2 = array(11.11,22.22);
$arg3 = array(11.11,22.22,33.33);
echo '<br><br>';
var_dump( vsprintf($format1,$arg1) );
echo '<br><br>';
var_dump( vsprintf($format11,$arg1) );
echo '<br><br><br>';
var_dump( vsprintf($format2,$arg2) );
echo '<br><br>';
var_dump( vsprintf($format22,$arg2) );
echo '<br><br><br>';
var_dump( vsprintf($format3,$arg3) );
echo '<br><br>';
var_dump( vsprintf($format33,$arg3) );
?>
EXERCISE
<?php
echo "Testing vsprintf() :<br>
basic functionality - using bool format.<br>";
// Initialise all required variables
$format = "format";
$format1 = "%b";
$format2 = "%b %b";
$format3 = "%b %b %b";
$arg1 = array(TRUE);
$arg2 = array(TRUE,FALSE);
$arg3 = array(TRUE,FALSE,TRUE);
echo '<br><br>';
var_dump( vsprintf($format1,$arg1) );
echo '<br><br>';
var_dump( vsprintf($format2,$arg2) );
echo '<br><br>';
var_dump( vsprintf($format3,$arg3) );
?>
EXERCISE
<?php
echo "Testing vsprintf() :<br>
basic functionality - using char format.<br>";
// Initialise all required variables
$format = "format";
$format1 = "%c";
$format2 = "%c %c";
$format3 = "%c %c %c";
$arg1 = array(65);
$arg2 = array(65,66);
$arg3 = array(65,66,67);
echo '<br><br>';
var_dump( vsprintf($format1,$arg1) );
echo '<br><br>';
var_dump( vsprintf($format2,$arg2) );
echo '<br><br>';
var_dump( vsprintf($format3,$arg3) );
?>
EXERCISE
<?php
echo "Testing vsprintf() :<br>
basic functionality - using exponential format.<br>";
// Initialise all required variables
$format = "format";
$format1 = "%e";
$format2 = "%e %e";
$format3 = "%e %e %e";
$arg1 = array(1000);
$arg2 = array(1000,2000);
$arg3 = array(1000,2000,3000);
echo '<br><br>';
var_dump( vsprintf($format1,$arg1) );
echo '<br><br>';
var_dump( vsprintf($format2,$arg2) );
echo '<br><br>';
var_dump( vsprintf($format3,$arg3) );
?>
EXERCISE
<?php
echo "Testing vsprintf() :<br>
basic functionality - using unsigned format.<br>";
// Initialise all required variables
$format = "format";
$format1 = "%u";
$format2 = "%u %u";
$format3 = "%u %u %u";
$arg1 = array(-1111);
$arg2 = array(-1111,-1234567);
$arg3 = array(-1111,-1234567,-2345432);
echo '<br><br>';
var_dump( vsprintf($format1,$arg1) );
echo '<br><br>';
var_dump( vsprintf($format2,$arg2) );
echo '<br><br>';
var_dump( vsprintf($format3,$arg3) );
?>
EXERCISE
<?php
echo "Testing vsprintf() :<br>
basic functionality - using octal format.<br>";
// Initialise all required variables
$format = "format";
$format1 = "%o";
$format2 = "%o %o";
$format3 = "%o %o %o";
$arg1 = array(021);
$arg2 = array(021,0347);
$arg3 = array(021,0347,0567);
echo '<br><br>';
var_dump( vsprintf($format1,$arg1) );
echo '<br><br>';
var_dump( vsprintf($format2,$arg2) );
echo '<br><br>';
var_dump( vsprintf($format3,$arg3) );
?>
EXERCISE
<?php
echo "Testing vsprintf() :<br>
basic functionality - using hexadecimal format.<br>";
// Initialising different format strings
$format = "format";
$format1 = "%x";
$format2 = "%x %x";
$format3 = "%x %x %x";
$format11 = "%X";
$format22 = "%X %X";
$format33 = "%X %X %X";
$arg1 = array(11);
$arg2 = array(11,132);
$arg3 = array(11,132,177);
echo '<br><br>';
var_dump( vsprintf($format1,$arg1) );
echo '<br><br>';
var_dump( vsprintf($format11,$arg1) );
echo '<br><br><br>';
var_dump( vsprintf($format2,$arg2) );
echo '<br><br>';
var_dump( vsprintf($format22,$arg2) );
echo '<br><br><br>';
var_dump( vsprintf($format3,$arg3) );
echo '<br><br>';
var_dump( vsprintf($format33,$arg3) );
?>