Workaround for Fgetcsv obtained in Linux with an empty string of array elements _php tutorial

Source: Internet
Author: User
Share an article about the fgetcsv in Linux to get the array element is an empty string of the solution to the tutorial, the need for friends can refer to the next ha.

When importing data using CSV, we typically use Windows systems, which are edited in Windows Excel using GBK.

But on the server, many use Linux server, the source program uses UTF-8, so it is easy to produce character encoding problem.

If you just transcode the CSV file to UTF-8 so that there is no problem on the Windows Server,

On RedHat5.5, in the array obtained with FGETCSV, if the contents of a column are Chinese, the array element corresponding to the column is an empty string, and English is normal.

At this point, you need to set the area:

The code is as follows Copy Code

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


The CSV file that is uploaded, usually edited by Excel, is GBK encoded,
While the source code is UTF-8, transcoding processing is required
File_put_contents ($new _file, Iconv (' GBK ', ' UTF-8 ', file_get_contents ($new _file)));

Ini_set (' auto_detect_line_endings ', true);
Setting area: Simplified Chinese, UTF-8 encoding
SetLocale (Lc_all, ' ZH_CN. UTF-8 ');
Open CSV file
$handle = fopen ($new _file, ' R ');
Remove column Header
$data _heads = Fgetcsv ($handle);


http://www.bkjia.com/PHPjc/445322.html www.bkjia.com true http://www.bkjia.com/PHPjc/445322.html techarticle share an article about the fgetcsv in Linux to get the array element is an empty string of the solution to the tutorial, the need for friends can refer to the next ha. When importing data using CSV, 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.