Encoding issues in excel files imported using Yii framework php_PHP tutorial

WBOY
Release: 2016-07-14 10:08:03
Original
887 people have browsed it

Today when developing, I need to import the data in the excel file into the relevant configuration file. I followed the method I often used in the past, first exporting excel into a csv file, and then using the fgetcsv function to read the content of the file, and then using the Yii framework. The CConfigure class is exported into a configuration file, and then during inspection, it is found that there are some extra backslashes in the middle of some values ​​in the exported array. These backslashes generally appear in Traditional Chinese or Japanese text.

At first, I checked the fields in the csv and excel files and found that there were no problems. Then I checked the saveAsString function in the CConfigure class and found that it used the var_export function in php to implement the function of importing the configuration file. , then, I used var_export to output a certain field that would cause problems, and found that backslashes were indeed added to the string, but when I directly used echo or var_dump to output, this did not happen. At this time, I understood that the problem was The problem lies in the var_export function.
After checking the relevant information, I found that the var_export function will automatically escape. Maybe this is why backslashes are automatically added to the string, but the string itself does not have special characters. Why does var_export still add it? Backslash, this puzzled me. I checked the binary encoding of the string in question and found that the ASCII code of the single character where the backslash appears is also a backslash, which means that it is possible Is var_export escaping for a single backslash character? I didn't understand the reason in the end.
But when I didn’t know how to solve this problem, I suddenly thought that the text encoding of my csv was gbk, because it was converted directly through excel, and then the file encoding I exported was in utf-8 format. At the same time, my code itself is also in UTF-8 format. Although I finally successfully converted the gbk string into UTF-8 for export through format conversion, problems may arise during the conversion process (this is what I thought at the time, but After thinking about it, because there have been problems using var_export in gbk format before, it should not be a problem during the conversion process. It should be a bug when var_export processes Chinese character strings in gbk format?), so I copied all the csv files. Convert to utf-8 format, re-import the file, and successfully solve the problem. www.2cto.com
In fact, in the process of solving it, I thought it might be an encoding problem very early, but I have never found the format problem of the csv file converted by excel, because the configuration file exported before did not have Chinese characters, so this type of problem has not appeared. problem, so I ignored it. However, although the problem was solved in the end, the cause of the problem itself is still unknown. It is speculated that it is most likely caused by the compatibility of gbk encoding and var_export. Sure enough, we should try to use utf-8 unified encoding during development. , should reduce many unknown errors.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477805.htmlTechArticleToday when developing, I need to import the data in the excel file into the relevant configuration file. I used it frequently in the past. Method, first export excel into a csv file, and then use the fgetcsv function...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!