note 49249 modified in function.fputcsv by danbrown
| From: | danbrown@php.net | Date: | Thu, 20 Nov 2008 14:41:13 +0000 |
| Subject: | note 49249 modified in function.fputcsv by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146754@lists.php.net to get a copy of this message | ||
What about cells that span multiple lines? This function allows for cells to contain newlines:
<?php
function fputcsv($handle, $row, $fd=',', $quot='"')
{
$str='';
foreach ($row as $cell)
{
$cell = str_replace($quot, $quot.$quot, $cell);
if (strchr($cell, $fd) !== FALSE || strchr($cell, $quot) !== FALSE || strchr($cell,
"\n") !== FALSE)
{
$str .= $quot.$cell.$quot.$fd;
}
else
{
$str .= $cell.$fd;
}
}
fputs($handle, substr($str, 0, -1)."\n");
return strlen($str);
}
?>
I found this reference on the web:
http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm
--was--
What about cells that span multiple lines? This function allows for cells to contain newlines:
function fputcsv($handle, $row, $fd=',', $quot='"')
{
$str='';
foreach ($row as $cell)
{
$cell = str_replace($quot, $quot.$quot, $cell);
if (strchr($cell, $fd) !== FALSE || strchr($cell, $quot) !== FALSE || strchr($cell,
"\n") !== FALSE)
{
$str .= $quot.$cell.$quot.$fd;
}
else
{
$str .= $cell.$fd;
}
}
fputs($handle, substr($str, 0, -1)."\n");
return strlen($str);
}
I found this reference on the web:
http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm
http://php.net/manual/en/function.fputcsv.php