說明
mixed
fscanf ( resource handle, string format [, mixed &...] )
fscanf() 函數和
sscanf() 相似,但是它從與
handle 關聯的檔案中接受輸入並根據特殊的
format(定義於 sprintf()
的文件中)來解釋輸入。若果只給此函數傳遞了兩個參數,解析後的值會被作為陣列返回。否則,若果提供了可選參數,此函數將返回被賦值的數目。可選參數必須用引用傳遞。
格式字串中的任何空白會與輸入流中的任何空白符合。這意味著甚至格式字串中的製表符
\t 也會與輸入流中的一個空格字元符合。
例子 1. fscanf() 例子
<?php $handle = fopen("users.txt","r"); while ($userinfo = fscanf($handle, "%s\t%s\t%s\n")) { list ($name, $profession, $countrycode) = $userinfo; //... do something with the values } fclose($handle); ?>
|
|
例子 2. users.txt 的內容 javier argonaut pe
hiroshi sculptor jp
robert slacker us
luigi florist it |
|
注:
在 PHP 4.3.0 之前,從檔案中讀入的最大字元數是
512(或是第一個 \n,看先碰到哪種情況)。從
PHP 4.3.0 起可以讀取任意長的行。
參見
fread(),fgets(),fgetss(),sscanf(),printf()
和 sprintf()。