Detect UTF-8/ISO-8859-1 encoding when reading csv files, re-encode them to UTF-8

This commit is contained in:
Rodney Figaro 2017-03-27 10:59:29 +02:00
parent b252f6ba5c
commit f62cf68e0a

View File

@ -16,6 +16,18 @@ class CSVReader
'error' => function($failed_line_number) {},
'readline' => function(ArrayExtended $cols) { return true; }
);
// if required, convert the file to the correct format
$f = fopen($file_path, 'r');
if ($f) {
$line = fgets($f);
$encoding = mb_detect_encoding($line, "UTF-8,ISO-8859-1", true);
fclose($f);
}
if ($encoding == 'ISO-8859-1') {
file_put_contents($file_path, utf8_encode(file_get_contents($file_path)));
}
}
function setExpectedHeader(array $expected_header)