Skip to content

Instantly share code, notes, and snippets.

@bhowe
Created April 19, 2014 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhowe/11088689 to your computer and use it in GitHub Desktop.
Save bhowe/11088689 to your computer and use it in GitHub Desktop.
generate a csv from array. It will handle escaping your data etc
function generateCsv($data, $delimiter = ',', $enclosure = '"') {
$handle = fopen('php://temp', 'r+');
foreach ($data as $line) {
fputcsv($handle, $line, $delimiter, $enclosure);
}
rewind($handle);
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
return $contents;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment