dongqishou7471 2013-07-15 16:05
浏览 24

PHP csv函数fputcsv

I am telling you this function "fputcsv" is buggy ! Why am i saying this ? I created a csv file with say 4 lines

l1data1;l1data2;l1data3;l1data4
l2data1;l2data2;l2data3;l2data4
l3data1;l3data2;l3data3;l3data4
l4data1;l4data2;l4data3;l4data4

ok now i simple copy this file into another file using fgetcsv and fputcsv like the following :

$handle = fopen($nameFile, "r") or die("Cannot open file:  ".$nameFile);
$temporaryFile = fopen($nameTempFile, "w") or die("Cannot open file:  ".$nameTempFile);
while (($data = fgetcsv($handle,"", ";")) !== false) {
    fputcsv($temporaryFile, $data,";");
}
fclose($temporaryFile);
fclose($handle);

output file :

l1data1;l1data2;l1data3;l1data4
l2data1;l2data2;"l2data3";l2data4
l3data1;l3data2;"l3data3";l3data4
l4data1;l4data2;"l4data3";l4data4

for some reason it is leaving double quotes in my third column for no obvious reason.

Now i do it with some old ways :

while ($data=fgets($handle)){
   fputs($temporaryFile, $data);
}

Output is like expected :

l1data1;l1data2;l1data3;l1data4
l2data1;l2data2;l2data3;l2data4
l3data1;l3data2;l3data3;l3data4
l4data1;l4data2;l4data3;l4data4

Explain this !

EDIT :

CodeOffreArtemis;CodeOffre42C;CodeOffreSagic;ServiceType;Rate;TypeInterface;Techno;Libelles;CodeOffreCristal
G8R1_ACAB;ABE;;ACA;18;Eth;A;ACo Max2;test
G8R1_ACAC;ABE;ACABUS;ACA;18;Eth;A;ACo 20MMax;
G8R1_ACAD;AKE;ACAD;ACA;2MMAX;Eth;A;ACo 2MMax;
G8R1_ACANBA;WAB;;ACA;18;Eth;A;DSL Access Max2 ACA Nu ligne active;X201
G8R1_ACANBC;WAB;;ACA;18;Eth;A;DSL Access Max2 ACA Nu sur pré-câblée;X201
G8R1_ACANBI;WAB;;ACA;18;Eth;A;DSL Access Max2 ACA Nu ligne inactive;X201
G8R1_ACANBPORA;QAB;;ACA;18;Eth;A;DSL Access Max2 ACA Nu avec portabilité L active;X201
G8R1_ACANBR;WAB;;ACA;18;Eth;A;DSL Access Max2 ACA Nu Nd de routage;X201
G8R1_ACANCA;WAB;ACANB;ACA;18;Eth;A;DSL Access 20MMax ACA Nu ligne active;X201

first line is the header rest are the data.

  • 写回答

1条回答 默认 最新

  • dongrong5189 2013-07-15 16:15
    关注
    fgetcsv($handle,"", ";");
    

    Looking here: http://www.php.net/manual/en/function.fgetcsv.php

    array fgetcsv ( resource $handle [, int $length = 0 [, string $delimiter = ',' [, string $enclosure = '"' [, string $escape = '\\' ]]]] )
    

    About that Length parameter:

    Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters). It became optional in PHP 5. Omitting this parameter (or setting it to 0 in PHP 5.0.4 and later) the maximum line length is not limited, which is slightly slower.

    I can't tell for sure but it looks from the code like you're just omitting the handle which, depending on version may be legal, but seems like it could be ambiguous also? Try adding a 0 parameter there perhaps?

    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像