开发者

CSV lines in array(), need to preserve line breaks

I have an array in the following format - it's essentially an array of preformatted CSV lines - key 0 and 1 in this example have a CSV value containing a line break.

Array
(
    [0] => 'foo,foo,foo,foo
    bar,foo,a:1:{i:0;s:4:"blah";}'
    [1] => 'bar,bar,bar,bar
    foo,bar,a:1:{i:0;s:4:"blah";}'
    [1] => 'bar,bar,bar,foo,bar,a:1:{i:0;s:4:"blah";}'
)

What I am doing next, is imploding that using \r\n as the glue, to generate the CSV file, 开发者_如何学Pythonthen writing it out. The problem is that the resultant file does not wrap the fields containing a line break in double quotes so that the line break may be preserved (or at least I am assuming it will).

The CSV file generated will be uploaded to a database or edited in Excel and the line breaks need to be preserved in the field.

Using fputcsv after exploding into the right format generates a CSV which has the line break and wrapped in a double quote viewed in Notepad or similar but not when opened in Excel.

  $parent_wholelines = array();
  foreach ($output_array as $wholeline) {
    $parent_wholelines[] = explode(',', $wholeline);
  }


One snippet from http://www.php.net/manual/en/function.fputcsv.php, maybe could help you. OR get a use of serialize: http://php.net/manual/en/function.serialize.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜