1

I am generating csv file using fputcsv function. The problem is that I need something like this:

110,"","","60114010100000453237001004","some string"

The code:

    for($i = 0; $i < count($list); $i++)
    {
        $input[$i][] = 110;
        $input[$i][] = "";
        $input[$i][] = "";
        $input[$i][] = "60114010100000453237001004";
        $input[$i][] = "some string";
    }

    $handler = fopen($fileName, 'w+');

    foreach($input as $fields)
    {
        fputcsv($handler, $fields);
    }

    fclose($handler);

And the output is:

110,,,60114010100000453237001004,"some string"

So there are some quotes missing. When I try to add quotes myself, I get doubled quotes like:

110,"""","""","""60114010100000453237001004""","some string"

This happen when the code is like:

$input[$i][] = '"' . "" . '"';

or:

$input[$i][] = '"' . "6011..." . '"';

I tried setting enclosure parameter as an empty string, but that's not allowed.

How can I solve this problem?

khernik
  • 2,059
  • 2
  • 26
  • 51
  • I've just played around a little with this function and i am affraid, you can not do that what you want with this. I think, you need to write your own function. Please refer: http://stackoverflow.com/questions/2489553/forcing-fputcsv-to-use-enclosure-for-all-fields – vaso123 Oct 15 '14 at 14:26
  • The output CSV shown is perfectly valid CSV and actually adheres to standards more than your desired output which arbitrarily decides to omit quotes around the first item in the row. – Mike Brant Oct 15 '14 at 14:45
  • Yeah, but I still need to do it my way...is there some solution? – khernik Oct 15 '14 at 14:57

0 Answers0