php 使用 fputcsv 时避免来自 csv 文件的默认引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11516811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Avoid default quotes from csv file when using fputcsv
提问by Justin John
Avoid default quotes from csv using fputcsv
使用 fputcsv 避免来自 csv 的默认引号
fputcsv($fp, $array, ',', '"'); // OR fputcsv($fp, $array);
In test.csv
在 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
回答by John C
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):
两个双引号用于转义双引号,因此看起来您的日期/时间已被引用。如果您只想获得正确引用的 CSV,您可以尝试删除任何现有的双引号(这可能有点暴力):
$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 fputcsvwants to put somethingaround 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.
Thefputcsv想要在一个带有空格的字符串周围放一些东西,所以如果你根本不需要引号,你要么需要解决这个问题,要么让你自己的函数来做你想做的事。这个问题下有很多很好的解决方案(编辑:这是一个不同的链接)。或者,在PHP 手册的注释中还有许多其他解决方案。
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
回答by Aaron Ratner
I know that this is an old question but I had a similar issue and ultimately gave up on fputcsvand 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 fputcsvbut 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 fputcsvand 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 fputcsvbut I don't think it's necessary since the result is the same):
我知道这是一个老问题,但我有一个类似的问题,最终放弃了fputcsv,只是echo用正确的标题编辑它来告诉浏览器下载文件。我知道这并没有专门解决这个问题,fputcsv但考虑到我在它上面花费的时间并意识到没有解决空间问题的方法(除了可能比问题应得的更多的工作,如果有可能的话),我采用了最简单的解决方案。如果目标是创建一个动态的、可下载的 CSV 文件,那么下面的代码可以非常轻松灵活地完成任务。我的问题是我需要根据我正在使用的服务的要求将所有字段用双引号括起来。但是有些字段有空格,这意味着单(或双)引号是由自动添加的fputcsv这意味着我得到了像"'this is a value'". 无论我尝试什么,它都不会正确输出数据。所以这解决了它(但是,它不使用,fputcsv但我认为没有必要,因为结果是相同的):
$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.
如果他们在这个特定的格式问题上遇到麻烦,希望这可以节省时间。

