Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
358 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)."
");

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...