这个 vsprintf()函数 PHP中有一个内置函数,用于将数组值显示为格式化字符串。数组元素将插入主字符串中的百分号(%)。根据格式将数组值显示为格式化字符串,并接受数组参数而不是可变数量的参数。功能 返回格式化字符串 虽然 vprintf() 输出格式化字符串 语法:
null
vsprintf (format, arr_arguments)
使用的参数: 该函数采用两个参数,如下所述-
- 格式: 它有必需的参数。它指定如何将字符串格式化为变量。可能的格式值如下:
- %% –返回百分号
- %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_参数: 在格式化字符串处插入的数组参数。
其他格式:
- – ->左对齐变量值。
- [0-9] ->数字的最大字符串长度。
- [0-9] ->变量值的最小字符串宽度。
- + ->用于[+或-](默认为负数)。
项目1: 指定程序的字符串空间。
php
<?php $str1 = "Geeks" ; $str2 = "Geeksforgeeksarticle" ; // print string-1 only echo vsprintf( "%s" , array ( $str1 )); // print string-15 space echo vsprintf( "%15s" , array ( $str1 )); // print string-1 with space echo vsprintf( "%-25s" , array ( $str1 )); // print string with zero echo vsprintf( "%020s" , array ( $str1 )); // print string with * symbol echo vsprintf( "%'*10s" , array ( $str1 )); // print string-2 echo vsprintf( "%s" , array ( $str2 )); // print string-2 with decimal point echo vsprintf( "%2.10s" , array ( $str2 )); // print string-2 with space echo vsprintf( "%30s" , array ( $str2 )); // print string-2 with zero echo vsprintf( "%030s" , array ( $str2 )); ?> |
输出:
Geeks GeeksGeeks 000000000000000Geeks*****GeeksGeeksforgeeksarticleGeeksforge Geeksforgeeksarticle0000000000Geeksforgeeksarticle
项目2: 驱动浮动程序 %f和%f 在PHP中的vsprintf()函数中。
php
<?php // %f and %F floating number in // vsprintf function in php $value1 = 789495321; $value2 = 8080907021; $value3 = 334422190; echo "%f (local) Floating: " ; // for %f Floating-point number // for local settings aware $txt = vsprintf( "%f %f %f" , array ( $value1 , $value2 , $value3 )); echo $txt ; echo "%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: 使生效 %d、 %u、%e和%e 在vsprintf()函数中。
php
<?php // 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 "%u UnSigned decimal number : " ; // %d UnSigned decimal number // where (0<=zero) $tt = vsprintf( "%u %u %u" , array ( $value1 , $value2 , $value3 )); echo $tt ; echo "%e Scientific notation : " ; // Scientific notation for lowercase $result = vsprintf( "%e %e %e " , array ( $value1 , $value2 , $value3 )); echo $result ; echo "%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: 使生效 %%、%b、%o、%x和%x 在PHP中的vsprintf()函数中。
php
<?php // 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 "%b ->binary number : " ; $tt = vsprintf( "%b %b" , array ( $value1 , $value2 )); echo $tt ; // Returns [%o] octal number echo "%o ->octal number : " ; $result = vsprintf( "%o %o " , array ( $value1 , $value2 )); echo $result ; // Returns [%x] Hexadecimal number[lowercase] echo "%x ->Hexadecimal number Lc : " ; $result = vsprintf( "%x %x" , array ( $value1 , $value2 )); echo $result ; // Returns [%X] Hexadecimal number[Uperercase] echo "%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: 使生效 %g%g和%c(ASCII) PHP中的vsprintf()函数。
php
<?php // 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 "%G shorter of %E and %f : " ; $tt = vsprintf( "%G %G" , array ( $value1 , $value2 )); echo $tt ; // ASCII value echo "%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
相关文章: PHP | vprintf()函数 参考资料: http://php.net/manual/en/function.vsprintf.php
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END