我正在尝试使用此代码解析csv文件
I am trying to parse a csv file using this code
if (($handle = fopen($csvFilePath, "r")) !== FALSE) { $c=0; $string=""; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { if($c>0) { if(($data[$email]!="") and ($data[$firstname]!="") and ($data[$lastname]!="")) { $string.='(1,'; $string.="'".$data[$email]."',"; $string.="'".$data[$firstname]."',"; $string.="'".$data[$lastname]."',"; $string.="'".Yii::app()->params['clientimporttext']."'"; $string.='),'; } } $c++; } fclose($handle); }与Windows csv一起正常工作. 但是当我从Mac创建一个csv并解析它不起作用时((它无法识别行尾)),
It is working fine with windows csv. but when i create a csv from mac and parse that it is not working.( it cant identify end of line),
当我用Mac打开相同的Windows csv并使用相同的代码进行解析时,它也不起作用. 但是当我将其另存为Windows csv时.再次起作用. 那么Mac csv和Windows csv之间的区别到底是什么? 这两个分隔符是否不同? 我应该如何更改代码才能使其同时工作?
also when i open the same windows csv with mac and parse using the same code it is not working. but when i save it as windows csv. it again works. so actually wat is the difference between mac csv and windows csv? Is the delimiter different for these two? How should i change the code to make it work with both?
推荐答案答案在PHP文档注释中:
The answer is in the PHP doc comments:
be2.php/manual/zh/function .fgetcsv.php
注意:如果在读取Macintosh计算机上或由Macintosh计算机创建的文件时PHP无法正确识别行尾,则启用 auto_detect_line_endings 运行时配置选项可能有助于解决问题.
Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.
问题在于Windows和类unix的计算机(如您的Mac)上的行尾之间的区别:windows ='\r\n'和unix = '\n'
The problem is in the difference between line endings on Windows and unix-like machines (as your mac is): windows ='\r\n' and unix = '\n'
解决方案:在打开文件之前添加以下行:
The solution: add this line before opening the file:
ini_set('auto_detect_line_endings',TRUE);更多推荐
Windows csv和mac csv有什么区别?
发布评论