分享一篇关于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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools