Skip to content

Instantly share code, notes, and snippets.

@Danack
Created June 22, 2013 17:36
Show Gist options
  • Save Danack/5841738 to your computer and use it in GitHub Desktop.
Save Danack/5841738 to your computer and use it in GitHub Desktop.
emitCSV fields
function emitCSV(array $fields, $delimiter = ',', $enclosure = '"', $mysql_null = false) {
$delimiter_esc = preg_quote($delimiter, '/');
$enclosure_esc = preg_quote($enclosure, '/');
$output = array();
foreach ($fields as $field) {
if ($field === null && $mysql_null) {
$output[] = 'NULL';
continue;
}
$output[] = preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field) ? (
$enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure) : $field;
}
return join($delimiter, $output) . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment