How to write quotes ("") when using fputcsv of PHP

Viewed 565

How can I write quotation marks ("") in csv files using fputcsv. I tried:

  • '""' the result is written to csv file as """""""

  • "\"\"" The result is written to the csv file as \"\"

1 Answers

fputcsv is escaping the double quotes because it uses a double quote as the default enclosure character for each field it writes. You can change which character fputcsv uses by passing a new character as a parameter to the function:

$fh = fopen('filename.csv', 'w');

$data = ['""'];
$delimiter = ','; // keep the delimiter as a comma
$enclosure = "'"; // set the enclosure to a single quote

fputcsv($fh, $data, $delimiter, $enclosure);

fclose($fh);

See: https://www.php.net/manual/en/function.fputcsv.php

Related