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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With