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

Source: Internet
Author: User
Tags field table

Today, I mainly want to write a php method to import csv files. 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:

Copy codeThe 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.

Copy codeThe Code is as follows:
<? Php
$ 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.
Copy codeThe Code is as follows:
<? Php
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 "<script type = \" text/javascript \ "> alert (\" File Format error. please upload it again! \ "); </Script> ";
Exit;
}
$ Handle = fopen ($ file ['tmp _ name'], "r ");
$ File_encoding = mb_detect_encoding ($ handle );
If ($ file_encoding! = 'Ascii '){
Echo "<script type = \" text/javascript \ "> alert (\" file Encoding Error. please upload it again! \ "); </Script> ";
Exit;
}
$ Row = 0;
$ Str = "";
$ Sy = "";
While ($ data = fgetcsv ($ handle, 1000 ,',')){
$ Row ++;
If ($ row = 0)
Continue;
$ Num = count ($ data );
For ($ I = 0; $ I <$ num; $ I ++ ){
$ Str = (string) $ data [$ I]. '| ';
$ Str = mb_convert_encoding ($ str, "UTF-8", "GBK"); // the source code is known as GBK and converted to UTF-8
$ Sy. = $ str; // what I have done here is complicated: Use '|' to combine the content in the csv file '|', because I imported the product information, based on user requirements
// Define the data to be imported.
}
}
If ($ sy) {$ sy = rtrim ($ sy, '| ');}
$ Arr = explode ('|', $ sy );
$ Key = array_slice ($ arr, 0, $ num); // This array is the title in the csv file, that is, the product id, title, selling point, and other data.
$ Skey = array ();
$ Length = array ();
$ Co = count ($ arr );
$ P = $ co/$ num; // obtain the length of the data to be retrieved
For ($ j = 0; $ j <$ p; $ j ++ ){
$ Offset = ($ J-1) * $ num; // offset, just like pagination, an array I retrieve here based on offset is the information of a commodity.
If ($ j = 0 ){
$ Length [] = array_slice ($ arr, 0, $ num );
} Else {
$ Length [] = array_slice ($ arr, $ num + $ offset, $ num); // retrieves the fields and items
}
}
$ Arrtitle = array ();
$ Arrfileds = array ();
$ Arrtagname = DB: select ('field identifier', 'field name')-> from ('field table')-> fetch_all ();
Foreach ($ arrtagname as $ value ){
$ Arrfileds [$ value ['fileds _ tags'] = $ value ['fileds _ name'];
}
Foreach ($ fileds as $ v)
{
$ Temarr = explode ('-', $ v );
If (isset ($ temarr [0]) &! Empty ($ temarr [0]) {
If (isset ($ temarr [1]) &! Empty ($ temarr [1]) {
If ($ temarr [1] = 'wenben '){
$ Arrtitle [] = $ arrfileds [$ temarr [0]. 'text ';
}
} Else {
If ($ temarr [0]! = 'Pic ') {// if the field to be retrieved is an image, remove it.
$ Arrtitle [] = $ arrfileds [$ temarr [0];
}
}
}

}

$ Skey = array ();
$ Order = array ();
$ Order [] = 'act _ tag ';
$ Order [] = 'Channel _ tag ';
$ Order [] = 'created _ time ';
$ Order [] = 'orderby ';
$ Rows = '';
$ F = $ co/$ num; // find the number of items
For ($ p = 0; $ p <count ($ arrtitle); $ p ++ ){
// You can find the data you need based on your needs and use the product field ID you need to identify the corresponding English logo in the table.
$ Skey [] = DB: select ('field identifier')-> from ('field table')-> where ('field name', '= ', $ arrtitle [$ p])-> fetch_row ();
$ Rows. = $ skey [$ p] ['field id']. '| ';
}
If ($ rows) {$ rows = rtrim ($ rows, '| ');}
If (! Empty ($ rows) {$ exrows = explode ('|', $ rows);} else {$ exrows = array ();}
$ Skeys = array_merge ($ order, $ exrows );
$ Count1 = count ($ skeys); // number of fields
If (! Empty ($ length )){
For ($ x = 1; $ x <$ f; $ x ++) {// you can find the number of times a commodity is recycled.
$ Orders = array ();
$ Orders [] = $ act_tag;
$ Orders [] = $ channel_tag;
$ Orders [] = time ();
$ Newlen = array_merge ($ orders, $ length [$ x]);
If ($ count1! = Count ($ newlen) {// if the length of the product field is different from the length of the product, it indicates that the user has not entered any field.
$ Newrs = array ();
Echo "<script type = \" text/javascript \ "> alert (\" <font color = # f00;> ". 'Check the nth ,'. ($ x-1 ). 'item! '.' Import failed! '. "</Font>"); </script> ";
Fclose ($ handle );
Exit ();
} Else {// start
$ Arrimport = array_combine ($ skeys, $ newlen); // if the two arrays are equal, I merge the arrays and change the date in the imported csv to the timestamp to store them in the database.
If (! Empty ($ arrimport ['start _ time']) {$ sta = strtotime ($ arrimport ['start _ time']);} else {$ sta = (int) 0 ;}
If (! Empty ($ arrimport ['end _ time']) {$ end = strtotime ($ arrimport ['end _ time']);} else {$ end = (int) 0 ;}
$ Arrtime = array ('start _ time' => $ sta, 'end _ time' => $ end );
If (! Empty ($ arrimport ['start _ time']) &! Empty ($ arrimport ['end _ time']) {
$ Newrs = array_merge ($ arrimport, $ arrtime );
} Else {
$ Newrs = array ();
Echo "<script type = \" text/javascript \ "> alert (\" <font color = # f00;> ". 'Check the nth ,'. ($ x-1 ). 'item! '.' Import failed! '. "</Font>"); </script> ";
Fclose ($ handle );
Exit ();
}
If (count ($ skeys) = count ($ newrs )){
DB: insert ('item table', array_values ($ skeys ))
-> Values (array_values ($ newrs ))
-> Execute ();
}
} // End
}
}
If ($ row-1 = (int) 0 ){
Echo "<script type = \" text/javascript \ "> alert (\" <font color = # f00;> ". 'the item you imported is empty! '. "</Font>"); </script> ";
} Else {
Echo "<script type = \" text/javascript \ "> alert (\" <font color = # f00;> ". 'imported successfully '. "<font color = # f00;> ". ($ row-1 ). "</font> ". 'item! '. "</Font> ");
}
Fclose ($ handle );
}
?>

The above is the csv import process that I need to do in my work. It may be different from your import method, but some code will always help you!
The following is a simple import:
Copy codeThe Code is as follows:
<Form enctype = "multipart/form-data" action = "import. php" method = "POST">
Import Template
<Label for = "SELECT file"> SELECT file: </label> <input name = "csv_goods" type = "file"/>
<Input type = "submit" value = "import" name = "import"/>
</Form>
<? Php
If (isset ($ _ POST ['import']) {

$ File = $ _ FILES ['csv _ goods '];

$ File_type = substr (strstr ($ file ['name'], '.'), 1 );

// Check the file format
If ($ file_type! = 'Csv '){
Echo 'file format is incorrect. please upload it again! ';
Exit;
}
$ Handle = fopen ($ file ['tmp _ name'], "r ");
$ File_encoding = mb_detect_encoding ($ handle );

// Check the file encoding
If ($ file_encoding! = 'Ascii '){
Echo 'file Encoding Error. please upload it again! ';
Exit;
}

$ Row = 0;
While ($ data = fgetcsv ($ handle, 1000 ,',')){
// Echo "<font color = red> $ row </font>"; // you can know the total number of rows.
$ Row ++;
If ($ row = 1)
Continue;
$ Num = count ($ data );
// The data of each cell in each row is output in sequence.
For ($ I = 0; $ I <$ num; $ I ++ ){
Echo $ data [$ I]. "<br> ";
// Process the data here
}
}

Fclose ($ 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.