fputcsv with tab delimiter

Stefan picture Stefan · Nov 7, 2014 · Viewed 7.7k times · Source

I would like to create a CSV document with a tab delimiter. I've tried a lot of things and searched all over the internet but there isn't a single site with a working solutions. This is the script I'm using right now, but it is exporting without a tab delimiter

    $data[] = array("item 1", "item 2");
    $data[] = array("item 3", "item 4");
    $export = fopen("/export/test.csv", "w");
    foreach ($data as $row) {
      fputcsv($export, $row, "\t");
    }
    fclose($export);

I've also tried different other things instead of \t like chr(9) but nothing worked. Any solutions for this problem?

Answer

NealVDV picture NealVDV · May 31, 2016

Was searching for a solution to the same problem. Saw the solution in the comments by Mark Baker

Use fwrite($export, "sep=\t".PHP_EOL); before writing any contents to the file.