PHP sprintf() functie
Voorbeeld
Vervang het percentage-teken (%) door een variabele die als parameter wordt doorgegeven:
<?php $number = 2; $str = "Shanghai"; txt = sprintf("Er zijn 쥕joen auto's in %s.", $number, $str); echo $txt; ?>
Definition and usage
The sprintf() function writes the formatted string to a variable.
arg1,arg2,++ The parameters will be inserted at the percentage sign (%) symbol in the main string. This function is executed step by step. At the first % symbol, insert arg1, insert arg2, etc.
Note:If there are more % symbols than arg If there are more parameters, you must use placeholders. Placeholders are located after the % symbol and consist of numbers and '\$'. See example 2.
Tip:Related functions: printf(), vprintf(), vsprintf(), fprintf(), and vfprintf()
Syntax
sprintf(format,arg1,arg2,arg++)
Parameter | Description |
---|---|
format |
Required. Specifies the string and how to format the variables within it. Possible format values:
Additional format values. Must be placed between % and the letter (for example %.2f):
Note:If multiple of the above format values are used, they must be used in the order specified above. |
arg1 | Required. Specifies what to insert format Parameters at the first % symbol in the string. |
arg2 | Optional. Specifies what to insert format Parameters at the second % symbol in the string. |
arg++ | Optional. Specifies what to insert format Parameters at the third and fourth % symbols in the string. |
技术细节
返回值: | 返回已格式化的字符串。 |
PHP 版本: | 4+ |
更多实例
例子 1
使用格式值 %f:
<?php $number = 123; $txt = sprintf("%f",$number); echo $txt; ?>
例子 2
使用占位符:
<?php $number = 123; $txt = sprintf("带两位小数:%1\$.2f <br>不带小数:%1\$u",$number); echo $txt; ?>
例子 3
对所有可能的格式值的演示:
<?php $num1 = 123456789; $num2 = -123456789; $char = 50; // ASCII 字符 50 是 2 // 注释:格式值 "%%" 返回百分号 echo sprintf("%%b = %b",$num1)."<br>"; // 二进制数 echo sprintf("%%c = %c",$char)."<br>"; // ASCII 字符 echo sprintf("%%d = %d",$num1)."<br>"; // 带符号的十进制数 echo sprintf("%%d = %d",$num2)."<br>"; // 带符号的十进制数 echo sprintf("%%e = %e",$num1)."<br>"; // 科学计数法(小写) echo sprintf("%%E = %E",$num1)."<br>"; // 科学计数法(大写) echo sprintf("%鑾u",$num1)."<br>"; // 不带符号的十进制数(正) echo sprintf("%鑾u",$num2)."<br>"; // 不带符号的十进制数(负) echo sprintf("%%f = %f",$num1)."<br>"; // 浮点数(视本地设置) echo sprintf("%%F = %F",$num1)."<br>"; // 浮点数(不视本地设置) echo sprintf("%%g = %g",$num1)."<br>"; // 短于 %e 和 %f echo sprintf("%%G = %G",$num1)."<br>"; // 短于 %E 和 %f echo sprintf("%%o = %o",$num1)."<br>"; // 八进制数 echo sprintf("%%s = %s",$num1)."<br>"; // String echo sprintf("%%x = %x",$num1)."<br>"; // Hexadecimaal (kleine letters) echo sprintf("%%X = %X",$num1)."<br>"; // Hexadecimaal (hoofdletters) echo sprintf("%%+d = %+d",$num1)."<br>"; // Tekenaanduiding (positief) echo sprintf("%%+d = %+d",$num2)."<br>"; // Tekenaanduiding (negatief) ?>
Voorbeeld 4
Demonstratie van string-annotaties:
<?php $str1 = "Hello"; $str2 = "Hello world!"; echo sprintf("[%s]",$str1)."<br>"; echo sprintf("[%8s]",$str1)."<br>"; echo sprintf("[%-8s]",$str1)."<br>"; echo sprintf("[%08s]",$str1)."<br>"; echo sprintf("[%'*8s]",$str1)."<br>"; echo sprintf("[%8.8s]",$str2)."<br>"; ?>