How to solve the garbled problem when importing csv files in php

Source: Internet
Author: User
This article mainly introduces how to solve the garbled problem when importing csv files in php. For more information about how to import csv files in php, I want to write a method to import csv files in php. All of them can be imported. However, I encountered two problems during the import. One was that a garbled problem occurred during the test while writing code on windows, and then solved the problem. Second, garbled characters occur when the file is submitted to the linux system. I didn't know why it was garbled. At first I thought it was an error in code svn submission. Finally, I asked a question in one of my groups, A friend is a phpcms engineer. He said that he encountered errors from Windows to Linux at the beginning. later, the cause of the error was garbled. Let's start with the question and see how to solve the two problems!

Problem 1:

Php reads csv files, there is no Chinese reading in windows, I immediately think of a function mb_convert_encoding (); make the following settings $ str = mb_convert_encoding ($ str, "UTF-8 ", "GBK"); then you can. Of course you can also use iconv (); make the following settings iconv ('gbk', "UTF-8 // Transcoder // IGNORE", $ str ); these two functions solve the problem of garbled code on windows.

Problem 2:

Php reads csv files, and Chinese characters cannot be read in linux. baidu and google find a solution.

You just added a line of code setlocale (LC_ALL, 'zh _ cn'). Yes, it's blind. This is simple. if you don't know, it may take a lot of time to solve this problem.
PHP setlocale () function explanation
Definition and usage

The setlocale () function sets region information (Region information ).

Region information is the language, currency, time, and other information of a geographical region. This function returns the current region settings. if it fails, false is returned.
The following are common regional identifiers used for data collection:

The code is as follows:
Zh_CN GB2312
En_US.UTF-8 UTF-8
Zh_TW BIG5
Zh_HK BIG5-HKSCS
Zh_TW.EUC-TW EUC-TW
Zh_TW.UTF-8 UTF-8
Zh_HK.UTF-8 UTF-8
Zh_CN.GBK GBK

For example,
UTF-8: setlocale (LC_ALL, 'en _ US.UTF-8 ′);
Simplified: setlocale (LC_ALL, 'zh _ cn ');

The setlocale () function is introduced because garbled characters occur when I import a csv file to linux, including mb_convert_encoding () and iconv () the two functions do not solve the final problem. Finally, this setlocale (LC_ALL, 'zh _ cn') is added. it is easy to get it done before the code that begins to import the csv file. then I found the information again, it is found that the fgetcsv () function is sensitive to region settings. For example, if LANG is set to a en_US.UTF-8, a single-byte encoded file will have a read error, so we need to set it to a regional. We will share it with you.

I still tried to use the following code, and these are settings for generating the header of the csv file. It may not work for me, but it may not work for you. So I sorted it out and tried my best to help me encounter garbled characters in the imported csv files, because it is really too difficult to handle in the case of no way. You can try it all! There is always one that belongs to you.

The code is as follows:
$ CsvContent = "csvzero, csvone, csvtwo, csvthree, csvfour, csvfive ";
Header ("Content-Type: application/vnd. ms-excel; charset = GB2312 ");
Header ("Pragma: public ");
Header ("Expires: 0 ");
Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
Header ("Content-Type: application/force-download ");
Header ("Content-Type: application/octet-stream ");
Header ("Content-Type: application/download ");
Header ("Content-Disposition: attachment?filename=csvdata .csv ");
Header ("Content-Transfer-Encoding: binary ");
$ CsvContent = iconv ("UTF-8", "gb2312", $ csvContent );
Echo $ csvContent;
Exit;
?>

Next let's take a look at the code for importing csv files in php:

Two functions are briefly introduced,

Mb_detect_encoding () detects the character encoding, or returns FALSE if the encoding of the specified string cannot be detected.

The fgetcsv () function reads a row from the file pointer and parses the CSV field. Similar to fgets (), the difference is that fgetcsv () parses the rows read and finds fields in CSV format, and then returns an array containing these fields. If an error occurs in fgetcsv (), FALSE is returned, including when the file ends.

Note: from PHP 4.3.5, the fgetcsv () operation is binary secure.

Note: empty rows in the CSV file will be returned as an array containing a single null field and will not be treated as an error.

Note: This function is sensitive to region settings. For example, if LANG is set to a en_US.UTF-8, a single-byte encoded file will have a read error.

Note: If PHP cannot recognize the row Terminator of the Macintosh File when reading the file, you can activate the auto_detect_line_endings runtime configuration option.
The code is as follows:
Setlocale (LC_ALL, 'zh _ cn'); // Set region information (Region information)
$ File = $ _ FILES ['Files'];
$ File_type = substr (strstr ($ file ['name'], '.'), 1 );
If ($ file_type! = 'Csv '){
Echo"

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.