note 109018 deleted from function.fputcsv by danbrown
| From: | danbrown@php.net | Date: | Wed, 13 Jun 2012 14:52:01 +0000 |
| Subject: | note 109018 deleted from function.fputcsv by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-188559@lists.php.net to get a copy of this message | ||
Note Submitter: Egbert van der Wal
----
The function array_to_CSV provided by Christine K - crimson at technologist dot com has a
shortcoming: if the CSV in question contains newlines it will fail because rewind($outstream) will
rewind to the start of the buffer, but fgets will just read until the first newline character.
A better solution to fix this is:
function array_to_CSV($data) {
$outstream = fopen("php://temp", 'r+');
fputcsv($outstream, $data, ',', '"');
$len = ftell($outstream);
rewind($outstream);
$csv = fread($outstream, $len);
fclose($outstream);
return $csv;
}