PHP中的vsprintf()函数是一个内置函数,用于将数组值显示为格式化字符串。数组元素将插入到主字符串中的百分号(%)处。根据其格式将数组值显示为格式化字符串,并接受数组参数代替可变数量的参数。该函数返回格式化的字符串。而vprintf()输出格式化的字符串
用法:
vsprintf (format, arr_arguments)
使用的参数:此函数采用两个参数,如下所述:
format:它具有Required参数。它指定如何将字符串格式化为变量。可能的格式值如下:%%-返回百分号
%b-二进制数
%d-带符号的十进制数字(负,零或正)
%u-无符号十进制数字(等于或大于零)
%X-十六进制数字(小写字母)
%X-十六进制数字(大写字母)
%F-浮点数(可识别本地设置)
%F-浮点数(不了解本地设置)
%o-八进制数
%C-根据ASCII值的字符
%s-弦
%e-使用小写字母的科学计数法(例如1.2e + 2)
%G-%e和%f中的较短者
%G-%E和%f中的较短者
%E-使用大写字母的科学计数法(例如1.2E + 2)
arr_arguments:在%符号格式的字符串处插入的数组参数。
附加格式:
---> Left-justifies用于变量值。
[0-9]->数字的最大字符串长度。
[0-9]->可变值的最小字符串宽度。
+->用于两个[+或-](默认情况下为负数)。
程序1:字符串空间指定程序。
$str1 = "Geeks";
$str2 = "Geeksforgeeksarticle";
// print string-1 only
echo vsprintf("%s\n", array(
$str1
));
// print string-15 space
echo vsprintf("%15s\n", array(
$str1
));
// print string-1 with space
echo vsprintf("%-25s\n", array(
$str1
));
// print string with zero
echo vsprintf("%020s\n", array(
$str1
));
// print string with * symbol
echo vsprintf("%'*10s\n", array(
$str1
));
// print string-2
echo vsprintf("%s\n", array(
$str2
));
// print string-2 with decimal point
echo vsprintf("%2.10s\n", array(
$str2
));
// print string-2 with space
echo vsprintf("%30s\n", array(
$str2
));
// print string-2 with zero
echo vsprintf("%030s", array(
$str2
));
?>
输出:
Geeks
Geeks
Geeks
000000000000000Geeks
*****Geeks
Geeksforgeeksarticle
Geeksforge
Geeksforgeeksarticle
0000000000Geeksforgeeksarticle
程序2:在PHP的vsprintf()函数中驱动浮动程序%f和%F。
// %f and %F floating number in
// vsprintf function in php
$value1 = 789495321;
$value2 = 8080907021;
$value3 = 334422190;
echo "\n%f (local) Floating:";
// for %f Floating-point number
// for local settings aware
$txt = vsprintf("%f %f %f", array(
$value1,
$value2,
$value3
));
echo $txt;
echo "\n%F (Not local) Floating:";
// for %F Floating-point number
// for local settings aware
$result = vsprintf("%F %F %F ", array(
$value1,
$value2,
$value3
));
echo $result;
?>
输出:
%f (local) Floating:789495321.000000 8080907021.000000 334422190.000000
%F (Not local) Floating:789495321.000000 8080907021.000000 334422190.000000
程序3:在vsprintf()函数中实现%d,%u,%e和%E。
// vsprintf function in php
// used %d, %u, %e, %E
$value1 = 7894;
$value2 = 9070;
$value3 = 3344;
echo "%d Signed decimal number:";
// %d Signed decimal number
// where (-, + or zero)
$txt = vsprintf("%d %d %d", array(
$value1,
$value2,
$value3
));
echo $txt;
echo "\n%u UnSigned decimal number:";
// %d UnSigned decimal number
// where (0<=zero)
$tt = vsprintf("%u %u %u", array(
$value1,
$value2,
$value3
));
echo $tt;
echo "\n%e Scientific notation:";
// Scientific notation for lowercase
$result = vsprintf("%e %e %e ", array(
$value1,
$value2,
$value3
));
echo $result;
echo "\n%E Scientific notation:";
// Scientific notation for uppercase
$result = vsprintf("%E %E %E ", array(
$value1,
$value2,
$value3
));
echo $result;
?>
输出:
%d Signed decimal number:7894 9070 3344
%u UnSigned decimal number:7894 9070 3344
%e Scientific notation:7.894000e+3 9.070000e+3 3.344000e+3
%E Scientific notation:7.894000E+3 9.070000E+3 3.344000E+3
程序4:在PHP中的vsprintf()函数中实现%%,%b,%o,%x和%X。
// vsprintf function in php
// used %%, %b, %o, %x, %X
$value1 = 789495;
$value2 = 334455;
// Returns [%] sign
echo "% ->Returns [%] sign:";
$txt = vsprintf("%% %%", array(
$value1,
$value2
));
echo $txt;
// Returns [%b] binary number
echo "\n%b ->binary number:";
$tt = vsprintf("%b %b", array(
$value1,
$value2
));
echo $tt;
// Returns [%o] octal number
echo "\n%o ->octal number:";
$result = vsprintf("%o %o ", array(
$value1,
$value2
));
echo $result;
// Returns [%x] Hexadecimal number[lowercase]
echo "\n%x ->Hexadecimal number Lc:";
$result = vsprintf("%x %x", array(
$value1,
$value2
));
echo $result;
// Returns [%X] Hexadecimal number[Uperercase]
echo "\n%X ->Hexadecimal number Uc:";
$result = vsprintf("%X %X", array(
$value1,
$value2
));
echo $result;
?>
输出:
% ->Returns [%] sign:% %
%b ->binary number:11000000101111110111 1010001101001110111
%o ->octal number:3005767 1215167
%x ->Hexadecimal number Lc:c0bf7 51a77
%X ->Hexadecimal number Uc:C0BF7 51A77
程序5:在PHP中实现%g%G和%c(ASCII)vsprintf()函数。
// vsprintf function in php
$value1 = 75;
$value2 = 55;
$char = 97;
$char2 = 69;
// shorter of %e and %f
echo "%g shorter of %e and %f:";
$txt = vsprintf("%g %g", array(
$value1,
$value2
));
echo $txt;
// %G - shorter of %E and %f
echo "\n%G shorter of %E and %f:";
$tt = vsprintf("%G %G", array(
$value1,
$value2
));
echo $tt;
// ASCII value
echo "\n%c ASCII value:";
$result = vsprintf("%c %c ", array(
$char,
$char2
));
echo $result;
?>
输出:
%g shorter of %e and %f:75 55
%G shorter of %E and %f:75 55
%c ASCII value:a E
参考:http://php.net/manual/en/function.vsprintf.php