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;
?>

Uitvoer voorbeeld

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:

  • %% - Returns a percentage sign %
  • %b - Binary number
  • %c - Character corresponding to ASCII value
  • %d - Decimal number with a sign (negative, 0, positive)
  • %e - Uses lowercase scientific notation (for example 1.2e+2)
  • %E - Uses uppercase scientific notation (for example 1.2E+2)
  • 蒝ecimal number without a sign (greater than or equal to 0)
  • %f - Floating-point number (local settings)
  • %F - Floating-point number (non-local settings)
  • %g - Shorter %e and %f
  • %G - Shorter %E and %f
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)

Additional format values. Must be placed between % and the letter (for example %.2f):

  • + (add + or - in front of the number to define the sign of the number. By default, only negative numbers are marked, positive numbers are not marked)
  • ' (specifies what to use as padding, default is space. It must be used with the width specifier. For example: '%x20s' (using 'x' as padding))
  • - (left-adjust the variable value)
  • [0-9] (specifies the minimum width of the variable value)
  • .[0-9] (specifies the number of decimal places or maximum string length)

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;
?>

Uitvoer voorbeeld

例子 2

使用占位符:

<?php
$number = 123;
$txt = sprintf("带两位小数:%1\$.2f
<br>不带小数:%1\$u",$number);
echo $txt;
?>

Uitvoer voorbeeld

例子 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)
?>

Uitvoer voorbeeld

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>";
?>

Uitvoer voorbeeld