使用fputcsv时避免使用csv文件中的默认引号 [英] Avoid default quotes from csv file when using fputcsv
问题描述
避免使用fputcsv将csv中的默认引号
Avoid default quotes from csv using fputcsv
fputcsv($fp, $array, ',', '"'); // OR fputcsv($fp, $array);
在test.csv中
testname,045645765789,"""04.07.2012 12:10:52"""
如何在此处避免上述引号?
How to avoid above quotes here?
像下面一样
testname,045645765789,04.07.2012 12:10:52
推荐答案
两个双引号用于转义双引号,因此您的日期/时间似乎已经被引用了.如果您只想获取正确报价的CSV,则可以尝试删除所有现有的双引号(这可能有点蛮力):
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"
fputcsv
希望将东西放在一个带有空格的字符串周围,因此,如果您根本不希望使用引号,则要么需要变通解决,要么要自己做一个函数你想要什么. 此问题(编辑:这是一个不同的链接).另外,在 PHP手册的注释中,还有许多其他解决方案.
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
这篇关于使用fputcsv时避免使用csv文件中的默认引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!