Solution to the problem that the array element obtained by fgetcsv in Linux is an empty string_PHP tutorial

WBOY
Release: 2016-07-20 11:02:56
Original
946 people have browsed it

Share a tutorial on the solution to the problem that the array element obtained by fgetcsv in Linux is an empty string. Friends in need can refer to it. ​

When using CSV to import data, we usually use the Windows system and use GBK to edit in Windows Excel.

But on the server, many use Linux servers, and the source program uses UTF-8, which can easily cause character encoding problems.

If you just transcode the CSV file to UTF-8, there will be no problem on the Windows server,

On RedHat5.5, in the array obtained with fgetcsv, if the content of a column is Chinese, the array element corresponding to the column is an empty string, while English is normal.

At this time, you need to set the area:

The code is as follows Copy code
 代码如下 复制代码

setlocale(LC_ALL, 'zh_CN.UTF-8');
代码如下


// 上传的CSV文件,通常是用Excel编辑的GBK编码,
// 而源代码是UTF-8,需要进行转码处理
file_put_contents($new_file, iconv('GBK', 'UTF-8', file_get_contents($new_file)));

//ini_set('auto_detect_line_endings', true);
// 设置区域:简体中文,UTF-8编码
setlocale(LC_ALL, 'zh_CN.UTF-8');
// 打开CSV文件
$handle = fopen($new_file, 'r');
// 取出列头
$data_heads = fgetcsv($handle);

setlocale(LC_ALL, 'zh_CN.UTF-8');
The code is as follows

//The uploaded CSV file is usually GBK encoded edited with Excel,
//The source code is UTF-8 and needs to be transcoded
file_put_contents($new_file, iconv('GBK', 'UTF-8', file_get_contents($new_file)));

//ini_set('auto_detect_line_endings', true);
// Setting area: Simplified Chinese, UTF-8 encoding
setlocale(LC_ALL, 'zh_CN.UTF-8');
//Open CSV file
$handle = fopen($new_file, 'r');
// Take out the column header
$data_heads = fgetcsv($handle);
http://www.bkjia.com/PHPjc/445322.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445322.htmlTechArticle
Share a tutorial on the solution to the problem that the array element obtained by fgetcsv in Linux is an empty string, if you need it Friends can refer to it. When using CSV to import data, we usually...
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!