Share a tutorial on how to solve the problem of empty string array elements obtained by fgetcsv in linux. if you need it, refer to it. when using CSV to import data, we usually use Windows, using GBK in Windows... share a tutorial on how to solve the problem of empty string array elements obtained by fgetcsv in linux. if you need it, refer to it.
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 as a UTF-8, it's okay on Windows Server, and on RedHat5.5, in the array obtained with fgetcsv, if the content of a column is Chinese, the array element corresponding to this column is a null string, while the English is normal. in this case, you need to set the region as follows:
Setlocale (LC_ALL, 'zh _ CN.UTF-8 ');
The PHP instance 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 the region: 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 );
Address:
Reprinted at will, but please attach the article address :-)