Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem with fgetcsv( ) and Unicode

Tags:

php

csv

fgetcsv

i have a code. on localhost i have not problem with reading csv file (with Unicode chars). but when upload code on host output is nothing. why? what is solution?

while (($data=fgetcsv($fin,5000,","))!==FALSE) 
{
 var_dump($data[0]);  //on host output is `string(0) ""` but on local i can see output
 var_dump($data[1]);  //$data[1] is integer and  i can see output
}
like image 434
user006779 Avatar asked Mar 20 '26 05:03

user006779


2 Answers

Note:

Locale setting is taken into account by this function. If LANG is e.g. en_US.UTF-8, files in one-byte encoding are read wrong by this function.

http://php.net/fgetcsv

One possible solution is to use setlocale().

like image 118
timdream Avatar answered Mar 21 '26 19:03

timdream


One such thing is the occurrence of the UTF byte order mark, or BOM. The UTF-8 character for the byte order mark is U+FEFF, or rather three bytes – 0xef, 0xbb and 0xbf – that sits in the beginning of the text file. For UTF-16 it is used to indicate the byte order. For UTF-8 it is not really necessary.

So you need to detect the three bytes and remove the BOM. Below is a simplified example on how to detect and remove the three bytes.

$str = file_get_contents('file.utf8.csv');
$bom = pack("CCC", 0xef, 0xbb, 0xbf);
if (0 == strncmp($str, $bom, 3)) {
    echo "BOM detected - file is UTF-8\n";
    $str = substr($str, 3);
}

That's all

like image 24
Jaro Avatar answered Mar 21 '26 21:03

Jaro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!