vprintf() 函數輸出格式化的字符串
數 format 是轉換的格式,以百分比符號 ("%") 開始到轉換字符結束。下面的可能的 format 值:
%% - 返回百分比符號
%b - 二進制數
%c - 依照 ascii 值的字符
%d - 帶符號十進制數
%e - 可續計數法(比如 1.5e+3)
%u - 無符號十進制數
%f - 浮點數(local settings aware)
%f - 浮點數(not local settings aware)
%o - 八進制數
%s - 字符串
%x - 十六進制數(小寫字母)
%x - 十六進制數(大寫字母)
看實例
*/
$str="hello"; //定義字符串
$number=123; //定義數值
vprintf("%s world. day number %u",array($str,$number)); //輸出格式化的字符串
//實例二
$num1=123; //定義數值1
$num2=456; //定義數值2
$txt=vsprintf("%f%f",array($num1,$num2)); //輸出格式化字符串到變量
echo $txt;
/*
printf() 函數輸出格式化的字符串。
語法
printf(format,arg1,arg2,arg++)
arg1, arg2, ++ 等參數將插入到主字符串中的百分號 (%) 符號處。該函數是逐步執行的。在第一個 % 符號中,插入 arg1,在第二個 % 符號處,插入 arg2,依此類推。
*/
$str="hello world"; //定義首尾有空格的字符串
printf($str); //輸出轉換後的結果
/*
sprintf() 函數把格式化的字符串寫寫入一個變量中。
語法
sprintf(format,arg1,arg2,arg++)
*/
$str="hello"; //定義字符串
$number=123; //定義數值
$txt= ("%s world. day number %u",$str,$number); //返回格式化字符串到變量
echo $txt; //輸出結果hello world. day number 123
$str="123456abcdedfbcdef"; //定義字符串
sscanf($str, "%[^a-z]", $result); //取其中的非大寫字母
printf("result=%s", $result); //輸出結果
/*以下代碼最終顯示11,並將"hello world"字符串寫入到test.txt文件*/
$str="hello"; //定義字符串
$file=fopen("test.txt","w"); //打開文件
echo vfprintf($file,"%s world",$str); //輸出數據到流並顯示結果11