sprintf() 是 PHP 的內置函數,可將格式化的字符串寫入變量。它返回一個格式化的字符串。 PHP 4 及以上版本支持 sprintf() 函數。
sprintf() 函數與 printf() 函數類似,但兩者的唯一區別是 sprint() 將輸出保存為字符串,而不是像 printf() 函數那樣在瀏覽器上顯示格式化消息。
注意:sprintf() 與回聲一起工作。 sprintf() 函數返回的格式化字符串在瀏覽器上通過 echo 打印,而 printf() 直接將輸出放在瀏覽器上。
用法
sprintf() 的語法如下:
sprintf (format, agr1, agr2, arg3...):string
這裏arg1、arg2、arg3等是sprintf()的參數。這些參數將插入帶有百分比 (%) 符號的主字符串中。在每個 % 符號處,參數將插入 one-by-one。
參數
format (mandatory)
此參數是強製性參數,用於指定字符串並描述如何格式化其中的變量。在這種情況下,隻有不包括 % 的簡單字符會被直接複製到結果字符串中,但帶有 % 符號的字符會獲取自己的參數。可能的格式值為:
說明符
參數 | 描述 |
---|---|
%% | 它返回一個百分號 (%)。 |
%b | 參數顯示為二進製數。 |
%C | 該參數被視為整數並表示為具有該 ASCII 的字符。 |
%d | 該參數被視為正整數並表示為十進製數。 |
%e | 小寫的科學記數法,例如 1.2e+2。精度說明符指定要打印的小數點後的位數。 |
%E | 類似於 e 說明符,但使用大寫的科學記數法,例如 1.2E+2。 |
%u | 該參數被視為整數並表示為無符號整數。 |
%F | 浮點數(語言環境感知) |
%F | 浮點數(非語言環境感知) |
%g | 這是一種通用格式。 |
%G | 類似於 g 說明符,但它使用 E 和 F。 |
%o | 表示為八進製數。 |
%s | 該參數被視為以及作為字符串呈現。 |
%X | 它表示為帶有小寫字母的十六進製數。 |
%X | 它也表示為十六進製數字,但使用大寫字母。 |
注意:c 類型說明符忽略寬度和填充。
類型處理
類型 | 說明符 |
---|---|
string | s |
integer | d, u, c, o, x, X, b |
double | G, g, E, e, F, f |
還存在一些附加的格式值,它們位於百分號 (%) 和字母之間。 (例如:%.2f)
下麵列出了這些附加格式值:
旗幟
旗幟 | 描述 |
---|---|
-- | 給定字段內的 Left-justify 和右對齊默認為 |
+ | 在數字前麵加上前綴 +ive 和 -ive 符號。默認情況下,負號僅放置在負數之前。 |
(空間) | 它是默認值,並用空格填充結果。 |
0 | 隻有 left-pads 編號為零和 s 說明符它也可以 right-pads 零。 |
'(字符) | 它用字符填充結果。 |
價值回報
sprint() 函數返回格式化的字符串。
支持版本
PHP 4 及以上版本支持此函數。
例子
下麵給出一些例子來學習sprintf()函數的實際實現。
範例1:簡單示例
<?php
$format = 'It is the basic example of PHP String function.';
$res = sprintf($format,);
echo $res;
?>
輸出:
It is the basic example of PHP String function.
範例2:變量聲明
<?php
$quantity = 1;
$language = 'sprintf';
$format = 'This is the %dst example of the %s function.';
$res = sprintf($format, $quantity, $language);
echo $res;
echo '</br>';
echo sprintf("this function works with echo.");
?>
輸出:
This is the 1st example of the sprintf function.
This function works with echo.
範例3:參數交換
讓我們看完整的例子 3 來理解參數交換的概念。
<?php
$num = 54;
$course = 'PHP training';
$year = 2018;
$format = 'There are %d students in %s batch in the %d year.';
echo $res = sprintf($format, $num, $course, $year);
?>
輸出:
There are 54 students in PHP training batch in the 2018 year.
在這裏,如果我們在格式字符串中交換占位符的順序,那麽它會給我們帶來問題。它與代碼中的參數順序不匹配。因此,占位符與參數的順序不匹配。讓我們看看下麵的代碼——
<?php
$num = 54;
$course = 'PHP training';
$format = 'There are %d students in %s batch in the %d year';
echo $res = sprintf($format, $course, $num, $year);
?>
輸出:
There are 0 students in 54 batch in the 2018 year
因此,如果我們想保留代碼原樣並希望正確指示占位符引用的參數,則將其寫成下麵給定的代碼-
<?php
$num = 54;
$course = 'PHP training';
$year = 2018;
$format = 'There are %2$d students in %1$s batch in the %3$d year';
echo $res = sprintf($format, $course, $num, $year);
?>
輸出:
現在,輸出與原始輸出相同。
There are 54 students in PHP training batch in the 2018 year
注意:在這種情況下,我們需要定義參數的位置以打印正確的代碼輸出。
範例4:指定填充字符
<?php
echo sprintf("%'.8d\n",1234);
echo '</br>';
echo sprintf("%'.08d\n",1234);
?>
輸出:
現在,上述填充字符代碼的輸出將類似於-
....1234 00001234
範例5:指定填充字符
<?php
$snum = 3259461827;
echo sprintf("%.2e", $snum); //Display scientific value 3.26e+9
echo '</br>';
echo sprintf("%'*6s\n", "Hi"); //Display ****Hi (It displays 6 character including asterisk (*) before the text.)
echo '</br>';
echo sprintf("%'*-6s\n", "Hi"); //Display Hi**** (It displays 6 character including asterisk (*) after the text.)
echo '</br>';
$fnum = 125.235;
echo sprintf("%f\n", $fnum); //Display 125.235000
echo '</br>';
echo sprintf("%.2f\n", $fnum); //Display 125.23 (It displays only 2 digits after decimal point.)
echo '</br>';
echo sprintf("%.0f\n", $fnum); //Display 125 only (It does not display the digits after decimal point.)
echo '</br>';
echo sprintf("%.8f\n", $fnum); //Display 125.23500000 (It displays 8 digits after decimal point including 0.)
?>
輸出:
現在,上述填充字符代碼的輸出將類似於-
3.26e+9 ****Hi Hi**** 125.235000 125.23 125 125.23500000
PHP中sprintf()和printf()函數的區別
sprintf() 和 printf() 函數的共同點是 sprintf() 函數借助 echo 來顯示文本,而 printf() 函數不需要 echo 來顯示文本。我們將在示例的幫助下展示這種差異。
示例
<?php
$str1 = 'We tried to printed on the browser directly using sprint() function.';
sprintf($str1);
$format = 'This string is print on the browser with the help of echo function.';
$str2 = sprintf($format);
echo $str2;
?>
輸出:
在這裏,我們可以看到變量$str1 存儲的文本並沒有通過sprintf() 函數直接打印在瀏覽器上,因此我們使用echo 來顯示str2 變量存儲的字符串。

現在,讓我們看看 printf() 函數的工作情況。
<?php
$str1 = 'This string is printed on the browser directly on the browser without using echo.';
printf($str1);
?>
輸出:

相關用法
- PHP String substr()用法及代碼示例
- PHP String strtr()用法及代碼示例
- PHP String strtolower()用法及代碼示例
- PHP String strspn()用法及代碼示例
- PHP String substr_count()用法及代碼示例
- PHP String strtoupper()用法及代碼示例
- PHP String strtok()用法及代碼示例
- PHP String substr_replace()用法及代碼示例
- PHP String strstr()用法及代碼示例
- PHP String str_replace()用法及代碼示例
- PHP String sscanf()用法及代碼示例
- PHP String substr_compare()用法及代碼示例
- PHP String strrpos()用法及代碼示例
- PHP String wordwrap()用法及代碼示例
- PHP String ucwords()用法及代碼示例
- PHP String localeconv()用法及代碼示例
- PHP String quoted_printable_encode()用法及代碼示例
- PHP String ucfirst()用法及代碼示例
- PHP String nl2br()用法及代碼示例
- PHP String vsprintf()用法及代碼示例
注:本文由純淨天空篩選整理自 PHP String sprintf() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。