pillarOfLight pillarOfLight - 4 months ago 18x
PHP Question

Make PHP exported CSV with UTF-8 character work on mac excel using commas

So I'm trying to export a

using PHP in which the contents contains
UTF-8 character
and I want the resultant csv to open in Excel smoothly (including Mac excel)

So there is an answer here: How can I output a UTF-8 CSV in PHP that Excel will read properly?

Checkout the top answer.

But then in order to implement that you need to use tabs to separate the fields instead of commas...Is there a way to achieve this while still using commas and not tabs and still have it work in OS X


Mostly to Mark Baker but everyone feel free to comment

Another code update

header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header("Cache-Control: no-store, no-cache");
header("Content-Disposition: attachment; filename=fileexport.csv");
echo "\xEF\xBB\xBF";
print "sep=,\n";
print $output;


fputcsv should work fine in this instance. Take the following example, where as the third parameter of fputcsv is the delimiter. By default it is , (comma), but you could also use "\t" for tab files. CSV files should be interpreted the same on either OS

if( $fh = fopen("output_file.csv","w") ){

    $put = array("column1, with comma","column2, with comma","column3" /*,"columnN"*/);