Home php教程 php手册 使用Yii框架php导入excel文件中的编码问题

使用Yii框架php导入excel文件中的编码问题

Jun 13, 2016 am 10:54 AM
excel php yii use exist import develop document frame of coding question

今天在开发的时候需要将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统一编码,应该会减少很多不明的错误。

 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles