PHP fgetcsv()関数
❮PHPファイルシステムリファレンス例
開いているCSVファイルから1行を読み取って出力します。
<?php
$file = fopen("contacts.csv","r");
print_r(fgetcsv($file));
fclose($file);
?>
定義と使用法
fgetcsv()関数は、開いているファイルの行を解析し、CSVフィールドをチェックします。
ヒント:fputcsv()関数も参照してください。
構文
fgetcsv(file, length, separator, enclosure)
パラメータ値
Parameter | Description |
---|---|
file | Required. Specifies the open file to return and parse a line from |
length | Optional. Specifies the maximum length of a line. Must be greater than the longest line (in characters) in the CSV file. Omitting this parameter (or setting it to 0) the line length is not limited, which is slightly slower. Note: This parameter is required in versions prior to PHP 5 |
separator | Optional. Specifies the field separator. Default is comma ( , ) |
enclosure | Optional. Specifies the field enclosure character. Default is " |
escape | Optional. Specifies the escape character. Default is "\\" |
技術的な詳細
戻り値: | 成功した場合はCSVフィールド、無効なファイルが指定された場合はNULL、その他のエラーおよびEOFの場合はFALSEの配列 |
---|---|
PHPバージョン: | 4.0以降 |
バイナリセーフ: | はい、PHP4.3.5では |
PHP変更ログ: | PHP5.3-エスケープパラメータを追加しました |
その他の例
例
CSVファイルの内容全体を読み取って出力します。
<?php
$file = fopen("contacts.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
❮PHPファイルシステムリファレンス