PHPのお勉強!

PHP TOP

fscanf

(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)

fscanfフォーマットに基づきファイルからの入力を処理する

説明

fscanf(resource $stream, string $format, mixed &...$vars): array|int|false|null

関数fscanf()sscanf() に似ていますが、stream が指すファイルから入力を取得し、 指定したフォーマット format に基づき解釈を行います。

フォーマット文字列におけるあらゆる空白は 入力ストリームのあらゆる空白にマッチします。 これはつまりフォーマット文字列のタブ (\t) すらも 入力ストリームの空白1個にマッチしてしまうことを意味します。

fscanf() をコールするたびに、ファイルから 1 行ずつ読み込みます。

パラメータ

stream

fopen() を使用して作成したファイルシステムポインタリソース。

format

string を解釈するフォーマット。 sprintf() のドキュメントにある説明と比べて、以下の違いがあります。

  • ロケールに対応していません。
  • FgG および b はサポートしていません。
  • D は十進数値を表します。
  • i は基数検出つきの整数値を表します。
  • n は処理する文字数を表します。
  • s は、空白文字を読み取ると停止することを示します。
  • argnum$ の代わりに * を指定すると、 この変換仕様に従った値の代入を抑制します。

vars

オプションで代入する値。

戻り値

この関数のパラメータが二つだけの場合、処理された値は配列として返されます。 他方、オプションのパラメータが指定された場合、 この関数は、代入された値の数を返します。 オプション引数は参照渡しとする必要があります。

string で利用可能な部分文字列よりも、 format で期待された部分文字列の数が多い場合は、null が返されます。 その他のエラーが発生した場合は、false が返されます。

例1 fscanf() の例

<?php
$handle
= fopen("users.txt", "r");
while (
$userinfo = fscanf($handle, "%s\t%s\t%s\n")) {
list (
$name, $profession, $countrycode) = $userinfo;
//... これらの値を使用して何か行う
}
fclose($handle);
?>

例2 users.txt の内容

javier  argonaut        pe
hiroshi sculptor        jp
robert  slacker us
luigi   florist it

参考

  • fread() - バイナリセーフなファイルの読み込み
  • fgets() - ファイルポインタから 1 行取得する
  • fgetss() - ファイルポインタから 1 行取り出し、HTML タグを取り除く
  • sscanf() - フォーマット文字列に基づき入力を処理する
  • printf() - フォーマット済みの文字列を出力する
  • sprintf() - フォーマットされた文字列を返す

add a note

User Contributed Notes 7 notes

up
18
yasuo_ohgaki at hotmail dot com
23 years ago
For C/C++ programmers.

fscanf() does not work like C/C++, because PHP's fscanf() move file pointer the next line implicitly.
up
7
Bertrand dot Lecun at prism dot uvsq dot Fr
17 years ago
It would be great to precise in the fscanf documentation
that one call to the function, reads a complete line.
and not just the number of values defined in the format.

If a text file contains 2 lines each containing 4 integer values,
reading the file with 8 fscanf($fd,"%d",$v) doesnt run !
You have to make 2
fscanf($fd,"%d %d %d %d",$v1,$v2,$v3,$v4);

Then 1 fscanf per line.
up
2
eugene at pro-access dot com
22 years ago
If you want to read text files in csv format or the like(no matter what character the fields are separated with), you should use fgetcsv() instead. When a text for a field is blank, fscanf() may skip it and fill it with the next text, whereas fgetcsv() correctly regards it as a blank field.
up
1
worldwideroach at hotmail dot com
19 years ago
Yet another function to read a file and return a record/string by a delimiter. It is very much like fgets() with the delimiter being an additional parameter. Works great across multiple lines.

function fgetd(&$rFile, $sDelim, $iBuffer=1024) {
$sRecord = '';
while(!feof($rFile)) {
$iPos = strpos($sRecord, $sDelim);
if ($iPos === false) {
$sRecord .= fread($rFile, $iBuffer);
} else {
fseek($rFile, 0-strlen($sRecord)+$iPos+strlen($sDelim), SEEK_CUR);
return substr($sRecord, 0, $iPos);
}
}
return false;
}
up
-1
nico at nicoswd dot com
11 years ago
If you want to parse a cron file, you may use this pattern:

<?php

while ($cron = fscanf($fp, "%s %s %s %s %s %[^\n]s"))
{

}

?>
up
-2
loco.xxx at gmail dot com
18 years ago
to include all type of visible chars you should try:

<?php fscanf($file_handler,"%[ -~]"); ?>
up
-3
robert at NOSPAM dot NOSPAM
21 years ago
actually, instead of trying to think of every character that might be in your file, excluding the delimiter would be much easier.

for example, if your delimiter was a comma use:

%[^,]

instead of:

%[a-zA-Z0-9.| ... ]

Just make sure to use %[^,\n] on your last entry so you don't include the newline.
To Top