PHP 字符串 sprintf()函數
sprintf()是PHP的內置函數,它將格式化的字符串寫入變量中。愛掏網 - it200.com它返回一個 格式化的字符串 。愛掏網 - it200.comPHP 4及以上版本支持sprintf()函數。愛掏網 - it200.com
sprintf()函數類似于printf()函數,但它們之間唯一的區(qū)別是sprint()將輸出保存到字符串中,而不是像printf()函數一樣在瀏覽器上顯示格式化的消息。愛掏網 - it200.com
注意:sprintf()可以與echo一起使用。愛掏網 - it200.comsprintf()函數返回的格式化字符串由echo在瀏覽器上打印,而printf()直接將輸出放在瀏覽器上。愛掏網 - it200.com
sprintf()的語法如下:
sprintf (format, agr1, agr2, arg3...) : string
在這里,arg1,arg2,arg3等是sprintf()的參數。愛掏網 - it200.com這些參數將用百分號(%)符號插入到主字符串中。愛掏網 - it200.com在每個%符號處,參數都會依次插入。愛掏網 - it200.com
參數
format (必需)
這個參數是一個必需的參數,它指定了字符串并描述了如何格式化其中的變量。愛掏網 - it200.com在其中,只有不包含%的簡單字符會直接復制到結果字符串中,但帶有%符號的字符會獲取自己的參數。愛掏網 - it200.com可能的格式值有:
Specifiers
參數 | 描述 |
---|---|
%% | 返回一個百分號(%)符號。愛掏網 - it200.com |
%b | 參數被表示為二進制數。愛掏網 - it200.com |
%c | 參數被作為整數對待,并以ASCII表示的字符呈現。愛掏網 - it200.com |
%d | 參數被作為正整數對待,并表示為十進制數。愛掏網 - it200.com |
%e | 以科學計數法表示,使用小寫字母e,例如1.2e+2。愛掏網 - it200.com精度修飾符用于指定小數點后要打印的位數。愛掏網 - it200.com |
%E | 類似于e修飾符,但科學計數法使用大寫字母E,例如1.2E+2。愛掏網 - it200.com |
%u | 參數被作為整數對待,并表示為無符號整數。愛掏網 - it200.com |
%f | 浮點數(與區(qū)域相關) |
%F | 浮點數(與區(qū)域無關) |
%g | 一般格式。愛掏網 - it200.com |
%G | 類似于g修飾符,但使用E和F。愛掏網 - it200.com |
%o | 以八進制數表示。愛掏網 - it200.com |
%s | 參數被作為字符串對待,并呈現為字符串。愛掏網 - it200.com |
%x | 以小寫字母表示的十六進制數。愛掏網 - it200.com |
%X | 以大寫字母表示的十六進制數。愛掏網 - it200.com |
注意: c類型說明符忽略寬度和填充。愛掏網 - it200.com
類型處理
Type | Specifiers |
---|---|
string | s |
integer | d, u, c, o, x, X, b |
double | G, g, E, e, F, f |
有一些其他的格式值也存在,在百分號(%)和字母之間放置。愛掏網 - it200.com(例如:%.2f)
以下是這些額外的格式值列表:
標志
標志 | 描述 |
---|---|
- | 在給定的字段中左對齊,默認情況下右對齊 |
+ | 在數字前面加上正負號。愛掏網 - it200.com默認情況下,負號僅放在負數前面。愛掏網 - it200.com |
(空格) | 這是默認值,它用空格填充結果。愛掏網 - it200.com |
0 | 僅用零填充數字,并且對于s說明符,也可以用零進行右側填充。愛掏網 - it200.com |
‘(字符) | 用字符填充結果。愛掏網 - it200.com |
返回值
sprint()函數返回格式化字符串。愛掏網 - it200.com
支持版本
這個函數受PHP 4及以上版本支持。愛掏網 - it200.com
示例
下面給出了一些示例,以學習sprintf()函數的實際應用。愛掏網 - it200.com
示例1: 簡單的示例
<?php
format = 'It is the basic example of PHP String function.';res = sprintf(format,);
echores;
?>
輸出:
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);
echores;
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,以理解參數交換的概念。愛掏網 - it200.com
<?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.
在這里,如果我們交換格式字符串中占位符的順序,那么就會對我們造成問題。愛掏網 - it200.com它與代碼中的參數順序不匹配。愛掏網 - it200.com因此,占位符與參數順序不匹配。愛掏網 - it200.com讓我們看看下面的代碼-
<?php
num = 54;course = 'PHP training';
format = 'There are %d students in %s batch in the %d year';
echores = 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 %2d students in %1s batch in the %3d year';
echores = sprintf(format,course, num,year);
?>
輸出:
現在,輸出與原始輸出相同。愛掏網 - it200.com
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()函數的區(qū)別
sprintf()和printf()函數的主要區(qū)別在于,sprintf()函數通過echo來顯示文本,而printf()函數不需要echo來顯示文本。愛掏網 - it200.com我們將通過以下示例展示這個區(qū)別。愛掏網 - it200.com
示例
<?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);
echostr2;
?>
輸出:
在這里,我們可以看到變量$str1存儲的文本并沒有由sprintf()函數直接在瀏覽器中打印出來,所以我們使用echo來顯示由str2變量存儲的字符串。愛掏網 - it200.com
現在,讓我們看看printf()函數的工作方式。愛掏網 - it200.com
<?php
str1 = 'This string is printed on the browser directly on the browser without using echo.';
printf(str1);
?>
輸出: