When using CSV to import data, we usually use Windows, and use GBK to edit in Windows Excel.
But on the server, many use Linux server, the source program uses UTF-8, so it is easy to produce character encoding problems.
If you just transcode the CSV file to a UTF-8, so there is no problem on the Windows Server,
In RedHat5.5, if the content of a column in the array obtained using fgetcsv is Chinese, the array element corresponding to the column is a null string, while the English is normal.
In this case, you need to set the region:
| The code is as follows: |
Copy code |
|
Setlocale (LC_ALL, 'zh _ CN.UTF-8 ');
The code is as follows:
// The uploaded CSV file, which is usually GBK encoded in Excel,
// While the source code is UTF-8, transcoding is required
File_put_contents ($ new_file, iconv ('gbk', 'utf-8', file_get_contents ($ new_file )));
// Ini_set ('auto _ detect_line_endings ', true );
// Set area: Simplified Chinese, UTF-8 encoding
Setlocale (LC_ALL, 'zh _ CN.UTF-8 ');
// Open the CSV file
$ Handle = fopen ($ new_file, 'r ');
// Retrieve the column header
$ Data_heads = fgetcsv ($ handle ); |