0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHP で SJIS CSV を読み込む2つの方法

Last updated at Posted at 2025-03-25

fopen

$handle = fopen('test.csv', 'rb');
stream_filter_append($handle, 'convert.iconv.CP932/UTF-8//TRANSLIT', STREAM_FILTER_READ);
while (($line = fgets($handle)) !== false) {
    $row = str_getcsv($line);
}

SplFileObject

$splFile = new SplFileObject('php://filter/read=convert.iconv.CP932%2FUTF-8%2F%2FTRANSLIT/resource=test.csv');
$splFile->setFlags(
    SplFileObject::READ_CSV |
    SplFileObject::READ_AHEAD |
    SplFileObject::SKIP_EMPTY |
    SplFileObject::DROP_NEW_LINE
);
foreach ($splFile as $row) {
}

どちらを使うか?

  • CSV ファイルに改行がある場合は SplFileObject
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?