201905131741PHP 函式 fgetcsv 解碼中文錯誤

PHP fgetcsv 碰到特定 UTF-8 中文就是會解碼錯誤,例如「巴哈姆特電玩資訊站」、「愛奇藝」。不知道跟 PHP: fgetcsv - Manual 裡這段有沒有關:

The locale settings are taken into account by this function. If LC_CTYPE is e.g. en_US.UTF-8, files in one-byte encodings may be read wrongly by this function.

沒法,轉成 HTML-ENTITIES 、解完、再轉回來:

<?php
  $str = file_get_contents('data.csv');
  $stream = fopen('php://memory', 'r+');
  fwrite($stream, mb_convert_encoding($str, 'HTML-ENTITIES', 'UTF-8'));
  rewind($stream);

  while($cells = fgetcsv($stream)) {
    foreach ($cells as &$cell) {
        $cell = mb_convert_encoding($cell, 'UTF-8', 'HTML-ENTITIES');
    }

    unset($cell);

    // ...
  }

  fclose($stream);
?>
回應
Google 站內搜尋
BloggerAds
累積 | 今日
loading......
    沒有新回應!





Powered by Xuite