Soul
Soul

Reputation: 83

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

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

Upvotes: 3

Views: 1661

Answers (1)

zeterain
zeterain

Reputation: 1140

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

Upvotes: 2

Related Questions