So, my code generates a CSV file using PHP\'s built-in fputcsv function.
For the delimiter, I use \',\'
(a comma).
For the enclosure, I use \'
I worked around this by inserting some bogus string characters, with a space, #@ @#, and then removing them. Here's a sample implementation:
//$exported is our array of data to export
$filename = 'myfile.csv';
$fp = fopen($filename, 'w');
foreach ($exported as $line => $row) {
if ($line > 0) {
foreach ($row as $key => $value) {
$row[$key] = $value."#@ @#";
}
}
fputcsv($fp, $row);
}
fclose($fp);
$contents = file_get_contents($filename);
$contents = str_replace("#@ @#", "", $contents);
file_put_contents($filename, $contents);
This encloses all fields in double quotes, including empty ones