This article mainly introduces the solution to the problem of garbled characters when importing csv files into PHP. Friends who need it can refer to it.
Today I mainly want to write a method for importing csv files into PHP. In fact, a search on the Internet A lot. All can be implemented how to import. But I encountered two problems when importing. One was that the test had garbled characters when writing code on Windows, and then it was solved. The second one is that when it was submitted to the Linux system, garbled characters occurred again. I didn't know the reason for the garbled code at first. At first I thought it was an error in the code svn submission. In the end, I asked a question in one of my groups. A friend of mine works in phpcms. He said that he encountered an error in svn submission from Windows. When submitting to Linux, errors always occurred at first. Later, the cause was found to be garbled characters. Let’s get right to the point and see how to solve these two problems!
Solution to the problem:
When PHP reads a csv file, Chinese cannot be read on Windows. I immediately thought of a function mb_convert_encoding(); make the following settings $str = mb_convert_encoding ($str, "UTF-8", "GBK"); Then that's it. Of course, you can also use iconv(); to set iconv('GBK',"UTF-8//TRANSLIT//IGNORE",$str); as follows to solve the problem of garbled characters on Windows.
Solution to problem two:
PHP reads the csv file, but the Chinese cannot be read on Linux. I found the solution after Baidu and Google.
Just added it One line of code setlocale(LC_ALL, 'zh_CN'); Yes, it will blind your eyes. It's that simple, and if you didn't know, you could spend a lot of time figuring it out.
PHP setlocale() function explanation
Definition and usage
setlocale() function sets regional information (regional information).
Regional information is language, currency, time and other information for a geographical area. This function returns the current locale, or false on failure.
The following are commonly used region identifiers for collecting data:
1 2 3 4 5 6 7 8 |
|
For example,
utf-8: setlocale(LC_ALL, 'en_US.UTF-8′);
Simplified: setlocale(LC_ALL, 'zh_CN');
The reason why I tell you about the setlocale() function is because when I imported the csv file into the Linux system, garbled characters occurred, including the use of mb_convert_encoding( ) and iconv() functions failed to solve the final problem. Finally, I added this sentence setlocale(LC_ALL, 'zh_CN'); in front of the code at the beginning of importing the csv file and it was easily done. Then I searched for information and found that the fgetcsv() function is sensitive to locale settings. For example, if LANG is set to en_US.UTF-8, single-byte encoded files will have read errors, so we need to set the culture. Specially shared with everyone.
I also tried to use the following code but could not get it. These are the header settings for generating csv files. It might not work for me, but it might work for you. So I sorted it out and tried my best to help colleagues who encountered garbled characters when importing csv files, because it was really difficult to deal with it when there was no other way. Everyone can try it! There is always one that belongs to you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Let’s take a closer look at the code for php to import csv files:
A brief introduction to the two functions,
The character encoding detected by mb_detect_encoding() , or returns FALSE when the encoding of the specified string cannot be detected.
fgetcsv() function reads a line from the file pointer and parses the CSV field. Similar to fgets(), except that fgetcsv() parses the read line and finds the fields in CSV format, then returns an array containing those fields. fgetcsv() returns FALSE on error, including when the end of file is encountered.
Note: As of PHP 4.3.5, the operation of fgetcsv() is binary safe.
Note: Empty lines 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 locale settings. For example, if LANG is set to en_US.UTF-8, single-byte encoded files will have read errors.
Note: If you encounter that PHP cannot recognize the line ending characters of Macintosh files when reading files, you can activate the auto_detect_line_endings runtime configuration option.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
The above is the csv import process that I need to do for my work. It may be different from your import method, but some codes will always be helpful to you!
The following is a simple import:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
The above is the entire content of this article. I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Introduction to the calling interface and common functions of PHP encapsulated curl
The above is the detailed content of How to solve the problem of garbled characters when importing csv files into PHP. For more information, please follow other related articles on the PHP Chinese website!