Linux Fgetcsv a solution to an empty string of array elements _php tips

Source: Internet
Author: User
But on the server, many use Linux server, the source program uses UTF-8, so it is easy to generate character encoding problems.

If you just transcoding the CSV file to UTF-8, then there is no problem on the Windows Server,

On RedHat5.5, in an array of fgetcsv, if the contents of a column are Chinese, the corresponding array element of the column is an empty string, while the English is normal.

At this point, you need to set the zone:

SetLocale (Lc_all, ' ZH_CN. UTF-8 ');
The code is as follows
Copy Code code as follows:

The uploaded csv file, usually edited in Excel, is GBK encoded,
And the source code is UTF-8, need to do the transcoding processing
File_put_contents ($new _file, Iconv (' GBK ', ' UTF-8 ', file_get_contents ($new _file));

Ini_set (' auto_detect_line_endings ', true);
Set region: Simplified Chinese, UTF-8 encoding
SetLocale (Lc_all, ' ZH_CN. UTF-8 ');
Open CSV file
$handle = fopen ($new _file, ' R ');
Remove column headers
$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.