Linux fgetcsv取得的数组元素为空字符串的解决方法_php技巧

WBOY
Release: 2016-05-17 09:14:13
Original
1015 people have browsed it

但服务器上,很多使用Linux服务器,源程序使用UTF-8,这样很容易产生字符编码的问题.

如果仅仅将CSV文件转码为UTF-8,这样在Windows服务器上没有问题,

而在RedHat5.5上,用fgetcsv取得的数组中,如果某列的内容是中文,则该列对应的数组元素为空字符串,而英文则正常.

这时,需要设置区域:

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);
Related labels:
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