Using the YII framework PHP Import encoding problem in Excel file _php tutorial

Source: Internet
Author: User
Tags yii
Today in the development of the need to import the Excel file data into the relevant configuration file, I follow the method used in the past, I first exported Excel to a CSV file, and then use the Fgetcsv function to read the contents of the file, through the YII framework of the Cconfigure class exported to a configuration file , and then, at the time of the check, found that some of the values in the exported array have some extra backslashes in the middle, which generally appear in traditional Chinese or Japanese text. I first checked the fields in the CSV and Excel files and found that there was no problem, then I looked at the saveasstring function in the Cconfigure class and found that he used the Var_export function in PHP to implement the import configuration file, and then, I used var_export to output a field where the problem occurred, I found that I did add a backslash in the string, and when I used the echo or var_dump output directly, I realized that the problem was on the Var_export function. After consulting the relevant information I found that var_export this function will automatically escape, perhaps this causes the string to automatically add a backslash, but the string itself does not have special characters, why Var_export still add a backslash, this let me baffled its solution, I looked at the 2-bit encoding of the problematic string, and found that the ASCII code of the single character of the place where the backslash occurred was also a backslash, which means that the var_export was escaped for a single backslash character when escaped? That's why I didn't get it at last. However, when I do not know how to solve this problem, I suddenly think that my CSV text encoding is GBK, because it is the format of the direct conversion through Excel, and then I exported the file encoding is utf-8 format, while my code itself is also utf-8 format, although I finally through the format conversion, The GBK string was successfully converted to utf-8 export, but there might be a problem during the conversion (as I was thinking, but then, since the use of Var_export in the GBK format has been problematic, it should not be a problem in the conversion process, it should be var_ Export a bug when processing a Chinese character string in GBK format? ), so I converted all CSV files into utf-8 format, re-import the file, successfully resolved. Www.2cto.com actually in the process of solving, very early thought may be the encoding problem, but has not found the Excel conversion CSV file format problem, because the previously exported configuration file does not have the Chinese characters, therefore did not appear this kind of problem, then ignored, But in the end although the problem is solved, but the cause of the problem itself is unclear, presumably becauseGBK encoding and Var_export compatibility caused problems, sure enough, should be in development or try to use Utf-8 unified encoding, should reduce many unknown errors.

http://www.bkjia.com/PHPjc/477805.html www.bkjia.com true http://www.bkjia.com/PHPjc/477805.html techarticle today in the development of the need to import the Excel file data into the relevant configuration file, I follow the method used in the past, first export Excel to a CSV file, and then use the Fgetcsv function ...

  • Related Article

    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.