Solution to empty strings for array elements obtained by fgetcsv in linux _ PHP Tutorial

Source: Internet
Author: User
In linux, the array element obtained by fgetcsv is a null string. Share a tutorial on how to solve the problem of empty strings in the array elements obtained by fgetcsv in linux. if you have any need, refer to the example below. When using CSV to import data, we usually share a tutorial on how to solve the problem of empty strings in the array elements obtained by fgetcsv in linux. if you need it, refer to the example below.

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:

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 );


Bytes. When using CSV to import data, we usually...

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.