通过php在csv单元格中创建回车 [英] Creating carriage returns in csv cell via php

查看:710
本文介绍了通过php在csv单元格中创建回车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态生成一个csv文件,其中一些单元格将包含多行,地址字段例如将需要分组到一个单一的地址单元格,而不是地址,城市,州等。所有是好的,但在过去两天,我试图插入\r,\r\\\
,\\\
,chr(10),chr(13),以及在代码中的回车创建回车我在寻找单元格内。所有这些都失败了,要么在我的csv作为\r等字面上打印,或者当我在代码中手动回车,它生成一个新行。

I'm trying to dynamically generate a csv file with some cells that will contain multiple lines, the address field for example will need to be grouped into a single "address" cell instead of address,city,state etc. All is going well and but for the last two days i've tried to insert \r, \r\n, \n, chr(10),chr(13), as well as a carriage return within the code to create the carriage return i'm looking for within the cell. All of these fail, either being literally printed in my csv as "\r" etc or when I do a manual carriage return in the code it generates a new row. I'm using this to create the breaks in my cells but it isn't working

$groupedCell = implode('\r',$data);

我很确定代码是否正确放置\r我想要一个返回但不是我正在寻找的实际回报。我尝试了一些不同的编码,但仍没有运气,我在开放办公室测试,我猜这可能是问题,但我认为它可以处理一个单元格内的回车,我还没有看到任何文档支持否则。感谢您阅读!

I'm pretty sure the code is correct as its placing \r where I would like a carriage return but not the actual return i'm looking for. I've tried some different encodings but still no luck, I am testing in Open Office which I guess could be the issue but I would assume it can handle carriage returns within a cell and I haven't seen any documentation to support otherwise. Thanks for reading!

推荐答案

您需要使用\r。在单引号字符串中不能使用转义字符( \')。 '\\\
'
'\r'是一个字符反斜杠,后跟 n r ,而\\\
\r
分别是换行符和回车符。

You need to use "\r". You can't use escaped characters (aside from \') in single quoted strings. '\n' and '\r' are a literal backslash followed by an n or r, while "\n" and "\r" are newlines and carriage returns respectively.

对于在CSV文件中插入新行,由您的实现决定。 CSV没有标准,因此您必须根据提供CSV数据的系统确定要使用的格式。有些人可能接受一个'\\\
'
序列,并将其解释为一个新行,其他人可能允许一个文字换行,只要单元格包含在引号中,而其他人不会接受新行。

As for inserting new lines in your CSV file, it's up to your implementation. There is no standard for CSV, so you'll have to figure out what format to use based on the system you're supplying the CSV data to. Some might accept a '\n' sequence and interpret it as a new line, others might allow a literal newline provided the cell is enclosed in quotes, and still others will not accept new lines at all.

这篇关于通过php在csv单元格中创建回车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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