php fputcsv 和封闭字段
我正要问与这里提出的问题相同的问题... 强制 fputcsv 对 *所有* 字段使用附件
问题是
当我使用 fputcsv 写出一行时 PHP 将添加到打开的文件句柄 任意列的封闭字符 它相信需要它,但会 保留其他列不带 外壳。
例如,您最终可能会得到 像这样的线
11,“鲍勃”,詹金斯,“200 main st. USA “,等等
缺少附加虚假空格 每个字段的末尾,有没有 强制 fputcsv 始终包含的方法 带有外壳的列(默认 到一个“)字符?
答案是:
不,fputcsv() 仅包含该字段 满足以下条件
/* enclose a field that contains a delimiter, an enclosure character, or a newline */
if (FPUTCSV_FLD_CHK(delimiter) ||
FPUTCSV_FLD_CHK(enclosure) ||
FPUTCSV_FLD_CHK(escape_char) ||
FPUTCSV_FLD_CHK('\n') ||
FPUTCSV_FLD_CHK('\r') ||
FPUTCSV_FLD_CHK('\t') ||
FPUTCSV_FLD_CHK(' ')
)
没有“始终包含”选项。
我需要创建一个 CSV 文件,将每个字段包含在内......什么是最好的解决方案?
提前致谢...
I was just about to ask the same questions as the question aksed here.... Forcing fputcsv to Use Enclosure For *all* Fields
The question was
When I use fputcsv to write out a line
to an open file handle, PHP will add
an enclosing character to any column
that it believes needs it, but will
leave other columns without the
enclosures.For example, you might end up with a
line like this11,"Bob ",Jenkins,"200 main st. USA
",etcShort of appending a bogus space to
the end of every field, is there any
way to force fputcsv to always enclose
columns with the enclosure (defaults
to a ") character?
The answer was:
No, fputcsv() only encloses the field
under the following conditions
/* enclose a field that contains a delimiter, an enclosure character, or a newline */
if (FPUTCSV_FLD_CHK(delimiter) ||
FPUTCSV_FLD_CHK(enclosure) ||
FPUTCSV_FLD_CHK(escape_char) ||
FPUTCSV_FLD_CHK('\n') ||
FPUTCSV_FLD_CHK('\r') ||
FPUTCSV_FLD_CHK('\t') ||
FPUTCSV_FLD_CHK(' ')
)
There is no "always enclose" option.
I need to create a CSV file will every field enclosed... What would be the best solution?
Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
推出你自己的函数 - 这并不难:(
上面是使用 unix 风格转义)
C.
Roll your own function - its not hard:
(above is using unix style escaping)
C.
解决方法:假设您的数据位于二维数组中,您可以附加一个强制引用的字符串,并且您确定该字符串不包含在您的数据中(此处为“#@ @#”),然后将其删除:
A workaround: Supposing you have your data in a 2-dimensional array, you can append a string that will force quoting and you are sure is not contained in your data ("#@ @#" here) and then remove it:
我也遇到了同样的问题,我已经解决了,如下。
我已在每个数组值中插入单引号。
这样当你用excel打开csv文件时,就不会再显示科学计数法E+15了。
在这里我和大家分享一下我的做法。
这对我有用,我希望你也能这么做。
问候
I have encountered the same problem, and I have solved it as follows.
I have inserted single quotes to each array value.
This way when you open the csv file with excel, the scientific notation E + 15 will no longer be displayed.
Here I share you as I did.
This worked for me, I hope you do too.
Regards