[php] fprintf() 函數把格式化的字符串寫到指定的輸出流(例如:文件或數據庫)。
語法
fprintf(stream,format,arg1,arg2,arg++)
參數 描述
stream 可選。規定在哪裡寫/輸出字符串。
format 必需。轉換格式。
arg1 必需。規定插到 format 字符串中第一個 % 符號處的參數。
arg2 可選。規定插到 format 字符串中第二個 % 符號處的參數。
arg++ 可選。規定插到 format 字符串中第三、四等等 % 符號處的參數。
參數 format 是轉換的格式,以百分比符號 ("%") 開始到轉換字符結束。下面的可能的 format 值:
%% - 返回百分比符號
%b - 二進制數
%c - 依照 ASCII 值的字符
%d - 帶符號十進制數
%e - 可續計數法(比如 1.5e+3)
%u - 無符號十進制數
%f - 浮點數(local settings aware)
%F - 浮點數(not local settings aware)
%o - 八進制數
%s - 字符串
%x - 十六進制數(小寫字母)
%X - 十六進制數(大寫字母)
範例
範例 1
<?php
$str = "Hello";
$number = 123;
$file = fopen("test.txt","w");
echo fprintf($file,"%s world. Day number %u",$str,$number);
?>
輸出:
27
以下文本將寫入 "test.txt":
Hello world. Day number 123
範例 2
<?php
$number = 123;
$file = fopen("test.txt","w");
fprintf($file,"%f",$number);
?>
輸出:
123.000000
範例 3
使用佔位符:
<?php
$number = 123;
$file = fopen("test.txt","w");
fprintf($file,"With 2 decimals: %1\$.2f\nWith no decimals: %1\$u",$number);
?>
以下文本將寫入 "test.txt":
With 2 decimals: 123.00
With no decimals: 123
公告版位
礁溪溫泉套房,位於宜蘭縣礁溪國小旁,近麥當勞,離礁溪火車站、首都客運、葛瑪蘭汽車客運約5分鍾車程,礁溪溫泉套房出租,您不需要再行添購傢俱,只需要帶幾件衣服,就能輕輕鬆鬆住進礁溪溫泉套房,天天在礁溪溫泉套房洗溫泉唷!意洽:游媽媽,電話0939711360,4500~4900元/月
- May 24 Tue 2011 09:29
[php] fprintf() 函數把格式化的字符串寫到指定的輸出流(例如:文件或數據庫)
全站熱搜
留言列表
發表留言