LoginSignup
12
11

More than 5 years have passed since last update.

PHPで600MBほどのSJISのCSVを読み込んでUTF-8として処理する場合のサンプルコード

Posted at

2012年ごろに書いたもののうろ覚え版。

ファイルサイズがデカすぎてmod_phpで扱うには問題があったことと、「全てのテキストを一括でエンコーディング変換しないと、変換ミスが発生する」ケースがあったため、この様な強硬策を取った。

// SJISじゃなくてSJIS-win、EUC-JPじゃなくてeucJP-winを使うべき
$from_encoding = 'SJIS-win';
$to_encoding = 'UTF-8';
$file_path = '/path_to_csv/file.csv';

// PHPコマンドで一括変換するという暴挙
$command = sprintf("echo mb_convert_encoding(file_get_contents('%s'), '%s', '%s');", $file_path, $to_encoding, $from_encoding);

// 変換済みの"出力"をプロセスハンドルとして握る
$handle = popen(sprintf('%s -r %s', escapeshellarg(sprintf('%s/php', PHP_BINDIR)), escapeshellarg($command)), 'rb');

// 後は普通のCSV処理
$in_body = false;    // フラグとしか使わないならフラグでよくね
while (($row = fgetcsv($handle, 4000)) !== FALSE) {
    if ($in_body === false) {
        $in_body = true;
        continue;
    }

    if ($row === [null]) {
        continue;
    }

    if (count($row) !== 3) {
        throw new \RuntimeException('Unexpected number of column');
    }

    var_dump($row);
}
fclose($handle);
12
11
1

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
12
11