使用Yii框架php导入excel文件中的编码问题
今天在开发的时候需要将excel文件中数据导入到相关的配置文件中,我按照过去经常使用的方法,先将excel导出成csv文件,然后使用fgetcsv函数读取文件的内容,通过Yii框架中的CConfigure类导出成配置文件,然后在检查的时候发现导出的数组中有些值中间有一些多余的反斜杠,这些反斜杠一般出现在繁体中文或者日文的文字中。
一开始我先检查了csv和excel文件中的字段,发现并没有什么问题,之后我查看了CConfigure类中的saveAsString函数,发现他使用了php中的var_export函数来实现导入配置文件的功能,然后,我又使用var_export输出会出现问题的某个字段,发现确实在字符串中添加了反斜杠,而我直接使用echo或者var_dump输出时,则没有这个情况发生,这个时候我明白问题是出在var_export这个函数上。
在查阅了相关的资料后我发现var_export这个函数会自动转义,也许是这个原因导致了字符串中自动添加了反斜杠,可是字符串本身并没有特殊字符,为什么var_export还是会添加反斜杠,这个让我百思不得其解,我查看了出现问题的字符串的2进制编码,发现出现反斜杠的地方本身的单个字符的ascii码也是反斜杠,也就是说可能是var_export在转义时针对单个反斜杠字符进行了转义?这个原因我最后也没有搞明白。
不过在我不知道如何解决这个问题的时候,我突然想到我的csv的文字编码是gbk的,因为是通过excel直接转的格式,然后我导出的文件编码是utf-8的格式,同时我的代码本身也是utf-8的格式,虽然我最后通过格式转换,成功把gbk的字符串转换成utf-8导出,但是在转换的过程中可能会出现问题(当时想的是这样,不过之后考虑了一下,因为之前在gbk格式下使用var_export已经出现了问题,因此应该不是转换过程中的问题,应该是var_export处理gbk格式的汉字字符串时产生的bug?),于是我将csv文件全部转换成utf-8格式,重新导入文件,成功解决。www.2cto.com
其实在解决的过程中,很早就想到可能是编码的问题,但是一直没有发现excel转换的csv文件的格式问题,因为之前导出的配置文件没有汉字,因此没有出现过这一类的问题,于是就忽略了,不过最后虽然解决了问题,但是问题本身的原因还是不明,推测极有可能是因为gbk编码和var_export的兼容导致的问题,果然开发中应该还是尽量使用utf-8统一编码,应该会减少很多不明的错误。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
