Solution to empty strings for array elements obtained by Linuxfgetcsv

Source: Internet
Author: User
When using CSV to import data, we usually use Windows system, using GBK in Windows Excel edit but on the server, a lot of use Linux server, source program using UTF-8, in this way, character encoding is easily generated.

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:

Setlocale (LC_ALL, 'zh _ CN.UTF-8 ');
The code is as follows:
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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.