Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP read UTF-8 CSV

I have a PHP script that reads from a CSV file, the file is in UTF-8 format and the code below is treating it as ASCII. How can I change the code to read the file as UTF-8?

if (($handle = fopen("books.csv", "r")) === FALSE)
  throw new Exception("Couldn't open books.csv");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

[EDIT] One of the issues with my current code is that the first value on the first line always has the three bytes that identifies UTF-8 files appended at the beginning. So I guess a solution that operates on a value by value or a row by row might not be good enough?

like image 420
Bishoy Avatar asked Nov 08 '22 02:11

Bishoy


1 Answers

Use fgets() get file all string in variable $date, then mb_convert_encoding() convert encoding, then str_getcsv() convert string to array.

if (($handle = fopen("books.csv", "r")) === FALSE)
    throw new Exception("Couldn't open books.csv");

$data = "";

// get file all strin in data
while (!feof($handle)) {
    $data .= fgets($handle, 5000);
}

// convert encoding
$data = mb_convert_encoding($data, "UTF-8", "auto");

// str_getcsv
$array = str_getcsv($data);
like image 56
Jin.C Avatar answered Nov 15 '22 11:11

Jin.C