note 103987 deleted from function.fputcsv by peehaa
| From: | peehaa@php.net | Date: | Fri, 09 Dec 2016 22:34:10 +0000 |
| Subject: | note 103987 deleted from function.fputcsv by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208363@lists.php.net to get a copy of this message | ||
Note Submitter: Christine K - crimson at technologist dot com
----
These two handy functions allow you to use fgetcsv with a string instead of a file handle and
fputcsv to convert an array to a string instead of writing to a file handle. Hopefully this
functionality will exist natively soon. Props to prior notes on these functions that led me to
these.
<?php
function array_to_CSV($data)
{
$outstream = fopen("php://temp", 'r+');
fputcsv($outstream, $data, ',', '"');
rewind($outstream);
$csv = fgets($outstream);
fclose($outstream);
return $csv;
}
function CSV_to_array($data)
{
$instream = fopen("php://temp", 'r+');
fwrite($instream, $data);
rewind($instream);
$csv = fgetcsv($instream, 9999999, ',', '"');
fclose($instream);
return($csv);
}
?>