PHP生成csv不发送正确的新换行 [英] PHP generating csv not sending correct new line feeds

查看:185
本文介绍了PHP生成csv不发送正确的新换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本使用以下代码生成csv文件:

I have a script that generates a csv file using the following code:

header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="'.date("Ymdhis").'.csv"');
print $content;

$ content变量只包含用逗号分隔的字段,然后使用\\\
;生成一个新行。

The $content variable simply contains lines with fields separated by commas and then finalised with ."\n"; to generate a new line.

当我在csv中打开文件看起来不错,但是,当我尝试使用该文件导入到外部程序(MYOB)不识别行尾(\\\
)字符,并假定一行长的文本。

When I open the file in csv it looks fine however, when I try to use the file to import into an external program (MYOB) it does not recognise the End Of Line (\n) character and assumes one long line of text.

当我在记事本中查看文件的内容时,

When I view the contents of the file in notepad, the end of line character (\n) is a small rectangle box which looks like the character code 0x7F.

如果我打开文件并将其重新保存在excel中,它将删除

If I open the file and re-save it in excel, it removes this character and replaces it with a proper end of line character and I can import the file.

我需要在PHP中生成什么字符,以便记事本将其识别为有效的End Of Line字符? (\ n)显然不能完成这项工作。

What character do I need to be generating in PHP so that notepad recognises it as a valid End Of Line character? (\n) obviously doesn't do the job.

推荐答案

使用\r\\\
。 (带双引号)

Use "\r\n". (with double quotes)

以上是回车+换行符的ascii字符。

The above is the ascii characters for carriage return + line feed.

历史上,这涉及早期电脑上的计算,当输出打印到纸张上并将电传头的支架返回到线路的开始时是一个单独的操作以通过打印机馈送线。您可以通过执行回车换行,并通过换行插入空行。

Historically this relates to the early days of computing on teletypes when the output was printed to paper and returning the carriage of the teletype head to the start of the line was a separate operation to feeding a line through the printer. You could overwrite lines by just doing a carriage return and insert blank lines by just a line feed. Doing both returned the head to the start of the line and fed it a new line to print on.

系统之间需要的不同之处 -

What precisely was required differed between systems -


  • 仅限换行: - 大多数Unix系统

  • 回车加换行符: - DEC amd MS-DOS

  • 仅返回: - 早期Apple / Mac操作系统

目前只是一个在Unix系统上的换行符。维基百科在此有一个好网页

So what you're generating at the moment is a newline on a Unix system only. Wikipedia has quite a good page on this.

实际上还有unix命令行工具来做转换。 unix2dos和dos2unix命令通过将换行符转换为换行符和回车符来转换ascii文本文件在unix和dos格式之间的转换。

There's actually unix command line tools to do the conversion too. The unix2dos and dos2unix commands convert ascii text files back and forward between the unix and dos formats by converting line feed to line feed plus carriage return and vica versa.

这篇关于PHP生成csv不发送正确的新换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆