linux中fgetcsv取得的数组元素为空字符串的解决方法
分享一篇关于linux中fgetcsv取得的数组元素为空字符串的解决方法的教程,有需要的朋友可以参考一下.
使用CSV导入数据的时候,我们通常用的是Windows系统,使用GBK在Windows的Excel中编辑.但服务器上,很多使用Linux服务器,源程序使用UTF-8,这样很容易产生字符编码的问题.如果仅仅将CSV文件转码为UTF-8,这样在Windows服务器上没有问题,而在RedHat5.5上,用fgetcsv取得的数组中,如果某列的内容是中文,则该列对应的数组元素为空字符串,而英文则正常.这时,需要设置区域如下:
setlocale(LC_ALL, 'zh_CN.UTF-8');
PHP实例代码如下:
// 上传的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);
本文地址:
转载随意,但请附上文章地址:-)

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

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

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)
