Avoid default quotes from csv file when using fputcsv

I know that this is an old question but I had a similar issue and ultimately gave up on fputcsv and just and just echoed it with the proper headers to tell the browser to download the file. I know this doesn't specifically address the issue with fputcsv but given the amount of time I spent on it and realized that there is no solution to the space issue (outside of what is likely more work than the problem deserves if it's even possible), I went with the simplest solution. If the goal is to create a dynamic, downloadable CSV file then the code below accomplishes the task quite easily and flexibly. My issue was that I needed ALL fields to be wrapped in double quotes as required by a service I'm using. But some fields had spaces which means a single (or double) quote is automatically added by fputcsv and that meant I got values like "'this is a value'". No matter what I tried it would not output the data correctly. So this solves it (however, it does NOT use fputcsv but I don't think it's necessary since the result is the same):

$filename="my_file.csv";
header("Content-Type: text/csv; charset=utf-8");
header("Content-Disposition: attachment; filename=$filename");

for ($i=0; $i<=10; $i++) {
    $value1 = "Here is some value";
    $value2 = "Here is another value";
    $value3 = "Another value";

    echo $value1 . ",";
    echo $value2 . ",";
    echo $value3;
    if ($i != 10) echo "\n"; //only add new line if it's not the last line
}

You can also choose to wrap values in double quotes if wanted like I did by doing something like this:

for ($i=0; $i<=10; $i++) {
    $value1 = "Here is some value";
    $value2 = "Here is another value";
    $value3 = "Another value";

    echo '"'. $value1 . "\",";
    echo '"'. $value2 . "\",";
    echo '"'. $value3 . "\"";
    if ($i != 10) echo "\n"; //only add new line if it's not the last line
}

Hopefully this saves someone time if they're having trouble with this particular formatting problem.


Two double-quotes are used to escape double-quotes, so it looks like your date/time is already quoted. If you just want to get a correctly quoted CSV, you could try removing any existing double-quotes (this might be a bit brute force):

$array = array('testname',045645765789,'"04.07.2012 12:10:52"');
$array = str_replace('"', '', $array);

fputcsv($fp, $array);

// Output: testname,79323055,"04.07.2012 12:10:52"

The fputcsv wants to put something around a string with a space in it, so if you want no quotes at all you'll either need to work around or make your own function to do what you want. There are lots of good solutions under this question (edit: this was a different link). Alternately there are a number of other solutions in the comments of the PHP manual.

Based on the first link, you could do:

$array = array('testname',045645765789,'"04.07.2012 12:10:52"');
$array = str_replace('"', '', $array);

fputs($fp, implode(',', $array)."\n");

// Output: testname,79323055,04.07.2012 12:10:52 

Tags:

Php

Fputcsv