Search code examples
phpcsvline-breaksfputcsv

Line-Breaks on PHP fputcsv


I have this code.

$value1 = '1';
$value2 = '2';
$value3 = '3';

$field = array ("field1", "field2", "field3");
$value = array ($value1, $value2, $value3);

$list = array ($field, $value);

$fp = fopen ('file.csv', 'w');

foreach ($list as $fields) {
   fputcsv ($fp, $fields);
}

fclose ($fp);

But the result is like this:

field1,field2,field3,1,2,3

And I want a line-break on the CSV, like this:

field1,field2,field3
1,2,3

And without doble-quotes, as showed.

Any sugestion? Thanks.


Solution

  • I ran the code and the result is exactly what you want. Which is:

    field1,field2,field3
    1,2,3
    

    There is a line break (0x0A) after "field3" and another line break after "3". IncredibleHat is correct, there is absolutely no comma after "field3".

    See the output at http://codepad.org/k6d0t8nU