(PHP 4 >= 4.0.1, PHP 5)
fscanf — 從文件中格式化輸入
說明
mixed fscanf ( resource$handle
, string $format
[, mixed &$...
] )
fscanf() 函數和
sscanf() 相似,但是它從與
handle
關聯的文件中接受輸入并根據指定的
format
(定義于 sprintf()
的文檔中)來解釋輸入。
格式字符串中的任何空白會與輸入流中的任何空白匹配。這意味著甚至格式字符串中的制表符 t 也會與輸入流中的一個空格字符匹配。
每次調用 fscanf() 都會從文件中讀取一行。
參數
handle
文件系統指針,是典型地由 fopen() 創建的 resource(資源)。
format
參數格式是 sprintf() 文檔中所描述的格式。
...
The optional assigned values.
返回值
如果只給此函數傳遞了兩個參數,解析后的值會被作為數組返回。否則,如果提供了可選參數,此函數將返回被賦值的數目。 可選參數必須用引用傳遞。
更新日志
版本 說明 4.3.0 在 PHP 4.3.0 之前,從文件中讀入的最大字符數是 512(或者第一個 n,看先碰到哪種情況)。從 PHP 4.3.0 起可以讀取任意長的行。
范例
Example #1 fscanf() 例子
<?php
$handle = fopen("users.txt", "r");
while ($userinfo = fscanf($handle, "%st%st%sn")) {
list ($name, $profession, $countrycode) = $userinfo;
//... do something with the values
}
fclose($handle);
?>
Example #2 users.txt 的內容
javier argonaut pe hiroshi sculptor jp robert slacker us luigi florist it
參見
fread() - 讀取文件(可安全用于二進制文件) fgets() - 從文件指針中讀取一行 fgetss() - 從文件指針中讀取一行并過濾掉 HTML 標記 sscanf() - 根據指定格式解析輸入的字符 printf() - 輸出格式化字符串 sprintf() - Return a formatted string