1

In my application I'm generating a csv file with some data using php. My code looks like shown below.

$fileHandle = fopen('test.csv', "w");
$list = array (
    array('aaa;cc;kk;', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

foreach ($list as $fields) {
    fputcsv($fileHandle, $fields);
}
fclose($fileHandle);

The data aaa;cc;kk; is written to separate cells (1st cell contains aaa, 2nd cell contains cc) instead of writing it to a single cell.

In the documentation, it is mentioned that, by default delimiter is comma. Why this values are getting separated with semicolon? How can I avoid that?

Thanks in advance.

Jenz
  • 8,280
  • 7
  • 44
  • 77
  • 3
    When you say "separate cells" do you mean how it looks in Excel? In the plain text CSV how does it look? – Matt S Oct 06 '17 at 13:29
  • @MattS. I mean how it looks in Excel. In plain text, csv is correct. – Jenz Oct 06 '17 at 13:39
  • 3
    Then the CSV is valid. This isn't a programming question but an Excel support question. You probably have to configure it to not treat ";" as a delimiter. – Matt S Oct 06 '17 at 13:41
  • Fields with embedded commas must be enclosed within double-quote characters – Ravi Chauhan Oct 06 '17 at 13:46

1 Answers1

-2

The delimiter can be set by using the third parameter of fputcsv():

int fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' [, string $escape_char = "\" ]]] )

Please change:

fputcsv($file,explode(';',$customerInfo));

to:

fputcsv($file,explode(';',$customerInfo), ";");

Please check the documentation before asking on Stack Overflow.

Ravi Chauhan
  • 1,409
  • 13
  • 26