在 PHP 中将 CSV 写入不带括号的文件
是否有本机函数或实体类/库用于将数组写入 CSV 文件中的一行而无需封装?如果没有为封装参数传入任何内容,fputcsv
将默认为 "
。Google 让我失望了(返回有关 fputcsv
的一大堆页面的结果) >),PEAR 的库或多或少与 fputcsv
执行相同的操作,
其工作方式与 fputcsv
完全相同,但当前允许字段保持不带引号的状态
:。 > 所需的“字段 1”、“字段 2”、field3hasNoSpaces
:字段 1、字段 2、field3hasNoSpaces
Is there a native function or solid class/library for writing an array as a line in a CSV file without enclosures? fputcsv
will default to "
if nothing is passed in for the enclosure param. Google is failing me (returning results for a whole bunch of pages about fputcsv
), and PEAR's libraries do more or less the same things as fputcsv
.
Something that works exactly like fputcsv
, but will allow the fields to remain unquoted.
currently: "field 1","field 2",field3hasNoSpaces
desired: field 1,field 2,field3hasNoSpaces
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
关于上述附件的警告是有效的,但您已经说过它们不适用于您的用例。
我想知道为什么你不能使用这样的东西?
The warnings about foregoing enclosures are valid, but you've said they don't apply to your use-case.
I'm wondering why you can't just use something like this?
与 chr() 函数一起使用:
works with chr() function:
fputcsv($file, $data, ';', chr(127));
fputcsv($file, $data, ';', chr(127));
好吧,
car(0)
并没有成功,因为NULL
值很可能会阻塞大多数 csv 解析器。我最终使用
fputcsv()
构建初始文件,然后检查并删除所有引号。优雅的?也许不是,但它完成了工作:)。Well
car(0)
didn't work out as theNULL
value will most likely choke most csv parsers.I ended using
fputcsv()
to build the initial file, then went through and removed all quotes. Elegant? Maybe not, but it got the job done :).这行不通吗?
Doesn't this work?
这就是我用来将标准 CSV 放入数组中的方法...
然后您可以在 foreach 循环中输出您想要的任何内容。
This is what I use to put standard CSV into an array...
You can then output whatever you want in a foreach loop.
没有附件的 CSV 文件的缺点是用户输入中的错误逗号会破坏该行。因此,您需要在写入 CSV 行之前删除逗号。
处理 CSV 的棘手部分是解析封装,这使得 PHP 和PEAR CSV 功能很有价值。本质上,您正在寻找一个以逗号分隔列、以换行符分隔行的文件。这是一个简单的起点:
The downside with a CSV file with no enclosures means an errant comma in user input will munge the row. So you'll need to remove commas before writing a CSV row.
The tricky part with handling CSV is parsing enclosures, which makes the PHP & PEAR CSV functions valuable. Essentially you're looking for a file that is comma-delimited for columns and newline-delimited for rows. Here's a simple starting point:
我使用棘手的方法来删除双引号,但仅限于 Linux
I use tricky way to remove double quote, but only in Linux
好的旧 fwrite() 有什么问题吗?
(显然你需要先确保$data中的数据被转义)
Whats wrong with good old fwrite()?
(Obviously you need to make sure the data in $data is escaped first)
根据您的应用选择解决方案。对于某些情况,需要自己的 csv 代码。对于情况 1(参见结果 chr0),情况 2(chr127) 和情况 3 (chr127) 数据将被修改(坏事)。相反,请使用类似的内容,谢谢@oops:
fputcsv($f, $array, $delimiter, car(0))
查看结果 chr0fputcsv($f, $array, $delimiter, car(127))
chr127
fputcsv($f, $array, $delimiter, ' ' )
onespace
哎呀或
Ansyori 或
零和一)产生
更好的结果。
Chose the solution depends on your application. For some case own code for csv is needed. In case 1 (See result chr0), case 2 (chr127) and case 3 (chr127) the data will be modified(bad thing). Instead use something like this, thanks @oops:
fputcsv($f, $array, $delimiter, car(0))
See result chr0fputcsv($f, $array, $delimiter, car(127))
chr127
fputcsv($f, $array, $delimiter, ' ')
onespace
oops or
Ansyori or
zeros-and-ones ) produces
better results.
chr(0)
也为我工作:chr(0)
also worked for me:这是一个老问题,但由于我也在努力解决这个问题,我认为让任何正在寻找类似内容的人知道比你可以在 enclosure 参数中传递空字符串会更好fputcsv 的,它根本不会使用任何外壳。
IE
This is an old question but as I was struggling with this as well, I thought it would be good to let anyone who is looking for something like this know than you can just pass empty string in the enclosure param of fputcsv and it will not use any enclosure at all.
i.e.
想通了。通过将 Null 的 ascii 代码传递给
car()
函数,它似乎工作得很好。fputcsv($f, $array, $delimiter, car(0))
感谢大家的回答!
Figured it out. By passing in the ascii code for Null to the
car()
function it seems to work just fine.fputcsv($f, $array, $delimiter, car(0))
Thanks for the answers everyone!!!