Most of the garbled characters in php are encoding problems. here we have analyzed for a long time the cause and solution of the following fgetcsv file garbled characters. The example code is as follows :? Phpfunctionget_csv _... most of the garbled characters encountered in php are encoding problems. here we have analyzed the cause and solution of the following fgetcsv file garbled characters. The example code is as follows:
$num fields in line $row:
n"; $row++; for ($c = 0; $c < $num; $c++) { echo $data[$c] . "
n";; /*echo getUTFString($data[$c])*/ } } fclose($handle);}
The imported csv file is saved in ansi encoding. for the Chinese operating system environment, it should be gbk encoding. by manually changing the browser character encoding to gbk, garbled characters disappear, at that time, the following adjustments were made. the code is as follows:
$ Data = eval ('Return '. iconv ('gbk', 'utf-8', var_export ($ data, true )).';');
$ Data is the array to be converted into encoding.
Supplement: linux fgetcsv garbled data reading GBK
When the Linux system uses the default settings, garbled characters will occur when the csv format files of gbk are processed on the Linux server.
Solution: Use the setlocale function to set the environment variables. for example, you can use the following statement before fgetcsv to set the region with gb. the code is as follows:
Setlocale (LC_ALL, array ('zh _ CN. gbk', 'zh _ CN. gb2312 ', 'zh _ CN. gb18030 ));
Which locale can be used? use the linux command locale-a to check what the system supports.