首頁  >  文章  >  後端開發  >  PHP 中的 sprintf

PHP 中的 sprintf

PHPz
PHPz原創
2024-08-29 12:34:54805瀏覽

php中的sprintf是一個函數,用於將格式化字串寫入變數並傳回格式化字串。在PHP中,版本4以上版本支援這個sprintf函數。 sprintf() 函數與 printf() 函數類似,但它們之間的主要且唯一的差異是 sprintf() 將輸出儲存到字串中,而不是像 printf() 函數那樣在瀏覽器上顯示格式化輸出。 sprintf() 函數可以與 echo 一起使用,即 sprintf() 傳回的格式化字串使用 echo 列印在瀏覽器上。讓我們更深入地研究這個主題,看看它的語法、可訪問的格式,並解決一些程式。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法

這是 PHP 中 sprintf() 函數的語法。

sprintf(format, arg1, arg2, arg3, …….)

這裡的arg1、arg2、arg3等是sprintf()的參數。 arg1 是必須插入到第一個參數的參數。 arg2, arg3, ……… 是要插入的選用參數。

Format:必填參數,指定如何格式化其中變數的字串。

以下是可能的格式:

參數

Parameter

Description

%b Argument present as a binary number
%% Returns % sign
%d Parameter treated as a positive integer, represented as a decimal number
%c Parameter treated as an integer, represented as a character with ASCII
%e Precision specifier that specifies the number of digits after the decimal point. Scientific notation with lowercase
%u Parameter treated as an integer, represented as unsigned integer
%f Floating-point number(locale)
%g General format
%o Represented as Octal number
%x Represented as Hexadecimal number with lowercase letters
%s Argument presented and treated as a string
%E Similar to %e specifier but with Uppercase.
%F Floating-point number(Non-locale)
%G Similar to %g specifier but uses %E and %F
%X Represented with Hexadecimal number but with uppercase

說明

%b 以二進制數表示的參數 %% 回傳%符號 %d 參數視為正整數,表示為十進制數 %c 參數視為整數,以 ASCII 字元表示 %e 精確度說明符,指定小數點後的位數。小寫科學記數法 %u 被視為整數的參數,表示為無符號整數 %f 浮點數(區域設定) %g 一般格式 %o 表示為八進制數 %x 表示為帶有小寫字母的十六進制數 %s 作為字串呈現並處理的參數 %E 類似於 %e 說明符,但大寫。 %F 浮點數(非語言環境) %G 與 %g 說明符類似,但使用 %E 和 %F %X 用十六進位數字表示,但要大寫 表>

There are some additional format values, which are placed between % and letter.

  • +, both + and – are forced in front of numbers. Negative numbers are marked by default.
  •  ‘ Specifies what is to be used as padding.
  •  – Left justifies the variable
  • [0-9] Specifies minimum width held to the variable.
  • .[0-9] Specifies the number of decimal digits or the maximum string length.

How does sprintf() function work in PHP?

Let us see How sprintf() function in PHP works with few examples,

Example #1

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$num1 = 321234;
$num2 = 860;
$text = sprintf("%f,%f",$num1, $num2);
echo $text;
?>
</body>
</html>

Output:

PHP 中的 sprintf

Here, we have taken two float values and using sprintf() function, scanned the variables, and using echo, have printed the floating values on the console.

Example #2: for floating point decimals

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$num1 = 4563;
$text = sprintf("With 3 decimals: %1\$.3f
<br>With no decimals: %1\$u <br>With single decimal: %1\$.1f",$num1);
echo $text;
?>
</body>
</html>

Output:

PHP 中的 sprintf

So here for floating values, we have specified as to no decimals, or single decimal, or 3 decimal values.

Example #3: with string specifiers

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$string1 = 'PHPv4';
echo sprintf("[%s]",$string1)."<br>";
echo sprintf("[%08s]",$string1)."<br>";
echo sprintf("[%-8s]",$string1)."<br>";
echo sprintf("[%8s]",$string1)."<br>";
echo sprintf("[%8.8s]",$string1)."<br>";
echo sprintf("[%'*8s]",$string1)."<br>";
?>
</body>
</html>

Output:

PHP 中的 sprintf

So based on the output, [%s] will return the string as it is

[%08s] will return string with zero padding [%-8s] will return string with left justification [%8s] will return string with the right justification [%8.8s] will return string with left justification, cuts of characters after a specific value [%’*8s] will return string with additional *

Example #4: with Argument swapping

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$num = 7;
$fruits = 'Mangoes';
$arg1 = 'The %2$s are %1$d in number';
echo sprintf($arg1, $num, $fruits);
?>
</body>
</html>

Output:

PHP 中的 sprintf

So here, format string supports argument swapping/ numbering.
Imagine if the placeholders in format string do not match the order of the arguments as shown above. And hence, we have indicated the arg1 which arguments refer to which placeholders.

Example #5: for all format specifiers

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$arg1 = 456;
$arg2 = -456;
$str = 57;
echo sprintf("%%b = %b",$arg1)."<br>";
echo sprintf("%%d = %d",$arg1)."<br>";
echo sprintf("%%d = %d",$arg2)."<br>";
echo sprintf("%%c = %c",$str)."<br>";
echo sprintf("%%e = %e",$arg1)."<br>";
echo sprintf("%%u = %u",$arg1)."<br>";
echo sprintf("%%u = %u",$arg2)."<br>";
echo sprintf("%%f = %f",$arg1)."<br>";
echo sprintf("%%f = %f",$arg2)."<br>";
echo sprintf("%%g = %g",$arg1)."<br>";
echo sprintf("%%g = %g",$arg2)."<br>";
echo sprintf("%%o = %o",$arg1)."<br>";
echo sprintf("%%o = %o",$arg2)."<br>";
echo sprintf("%%x = %x",$arg1)."<br>";
echo sprintf("%%x = %x",$arg2)."<br>";
echo sprintf("%%s = %s",$arg1)."<br>";
echo sprintf("%%s = %s",$arg2)."<br>";
echo sprintf("%%E = %E",$arg1)."<br>";
echo sprintf("%%F = %F",$arg1)."<br>";
echo sprintf("%%G = %G",$arg1)."<br>";
echo sprintf("%%X = %X",$arg1)."<br>";
echo sprintf("%%+d = %+d",$arg1)."<br>";
echo sprintf("%%+d = %+d",$arg2)."<br>";
?>
</body>
</html>

Output:

PHP 中的 sprintf

So here we have shown all the format specifiers.

With this, we shall conclude the topic ‘sprintf in php’. We have seen the syntax of sprintf() function in PHP. We have seen what each format specifier means and have Illustrated few examples on how to use sprintf in PHP. The above examples will give a clear understanding of all the format specifiers.

以上是PHP 中的 sprintf的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP 中的變數下一篇:PHP 中的變數