18

Avoid default quotes from csv using fputcsv

fputcsv($fp, $array, ',', '"'); // OR fputcsv($fp, $array);

In test.csv

testname,045645765789,"""04.07.2012 12:10:52"""

How to avoid above quotes here?

Like below

testname,045645765789,04.07.2012 12:10:52
Justin John
  • 9,223
  • 14
  • 70
  • 129
  • Seems that the reason you would get three `"` quotes is because that date string is already surrounded by `"` quotes. (See the [example in the docs](http://us2.php.net/fputcsv#example-2230).) Remove the quotes before passing the array to `fputcsv()`, or `implode()` with commas manually instead of using `fputcsv()`. – Wiseguy Jul 17 '12 at 06:14
  • 1
    Yes, But there will one quote due to space in string `testname,045645765789,"04.07.2012 12:10:52"`. How to avoid that? – Justin John Jul 17 '12 at 06:18

2 Answers2

22

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 
zen
  • 980
  • 6
  • 18
John C
  • 8,223
  • 2
  • 36
  • 47
  • 1
    Yes date/time is already quoted, if I avoid that quotes, still there will one quote left like `testname,045645765789,"04.07.2012 12:10:52"` due to space in string . How to avoid that? – Justin John Jul 17 '12 at 06:22
  • As I say in my answer, with `fputcsv` you can't really avoid that. Have a look at the links I posted for a range of workarounds. – John C Jul 17 '12 at 06:30
  • @JustinJohn My apologies, the first link I posted was different to my intention (corrected now). I've corrected it and added some code that demonstrates one way to achieve what you are after. – John C Jul 17 '12 at 12:22
  • 1
    Thanks, it works. Is it possible to reproduce same result with `fputcsv` instead of `fputs`? – Justin John Jul 17 '12 at 12:41
  • 7
    Great stuff! In short, you can not. `fputcsv` will *always* enclose a string with a space in it with something. – John C Jul 17 '12 at 12:50
  • Thank you very much. I searched for this around half a day. But finally you gave me the solution though! – BharathRao Apr 05 '19 at 07:32
  • Great ! short and sweet answer. Thanks – Sumit patel Nov 04 '19 at 05:17
1

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.

Aaron Ratner
  • 111
  • 3