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
2
  • 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.) Remove the quotes before passing the array to fputcsv(), or implode() with commas manually instead of using fputcsv(). Commented Jul 17, 2012 at 6: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? Commented Jul 17, 2012 at 6:18

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

6 Comments

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?
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.
@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.
Thanks, it works. Is it possible to reproduce same result with fputcsv instead of fputs?
Great stuff! In short, you can not. fputcsv will always enclose a string with a space in it with something.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.