PHP uses Fgetcsv to read the CSV file garbled solution,
The example in this article explains how PHP uses Fgetcsv to read the CSV file garbled. Share to everyone for your reference. The specific analysis is as follows:
Generally speaking in PHP garbled is mostly coding problem, here we analyze the Fgetcsv read CSV file garbled reason and solution.
Examples are as follows:
Copy the Code code as follows: function get_csv_contents ($file _target) {
$handle = fopen ($file _target, ' R ');
while ($data = Fgetcsv ($handle, 1000, ",")) {
$num = count ($data);
echo "
$num Fields $row:
n ";
$row + +;
for ($c =0; $c < $num; $c + +) {
echo $data [$c]. "
n ";;
/*echo getutfstring ($data [$c]) */
}
}
Fclose ($handle);
}
Imported CSV files are saved in ANSI encoding, for the Chinese operating system environment should be GBK encoding, by manually changing the browser character encoding for GBK, garbled situation disappeared, when the following adjustments.
Copy the code as follows: $data = eval (' Return '. Iconv (' GBK ', ' Utf-8 ', Var_export ($data, True)).
$data is an array that needs to convert the encoding.
Add: LINUX fgetcsv read GBK data garbled
When the Linux system is used by the default settings, the Linux server on the GBK CSV format file processing, there will be garbled phenomenon.
The workaround is to:
Use the setlocale function to set environment variables. For example, to set a locale using GB you can use the following statement before Fgetcsv.
Copy the Code code as follows: SetLocale (Lc_all,array (' zh_cn.gbk ', ' zh_cn.gb2312 ', ' zh_cn.gb18030 '));
Use the Linux command locale-a to see which locales are supported by the system.
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/908175.html www.bkjia.com true http://www.bkjia.com/PHPjc/908175.html techarticle PHP use fgetcsv read CSV file garbled solution, this example describes the PHP use fgetcsv read CSV file garbled solution. Share to everyone for your reference. Specific ...