Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php fputcsv use semicolon separator in CSV

I wrote a code where I retrieve data from DB and populate them in CSV using the function 'fputcsv'

I put on top the following:

$file = fopen("internal/customer_info.csv","w");

Then I retrieve the data and put them in variables, run the function:

$customerInfo = $first_name.";".$last_name.";".$address1.";".$address2.";".$postcode.";".$city.";".$country.";".$email;
fputcsv($file,explode(';',$customerInfo));

And finally, I closed the file.

My question is: How can I put a semicolon separator? As you can see I have semicolon there but the CSV output does not. It shows a comma instead.

Why's that? Can anyone help me?

like image 300
shieldcy Avatar asked Aug 31 '15 10:08

shieldcy


People also ask

How do I handle line breaks in a CSV file using PHP?

use implode(PHP_EOL) will do the trick.

How does PHP Fputcsv work?

fputcsv() formats a line (passed as a fields array) as CSV and writes it (terminated by a newline) to the specified file stream .

How to write to a CSV file in PHP?

To convert an array into a CSV file we can use fputcsv() function. The fputcsv() function is used to format a line as CSV (comma separated values) file and writes it to an open file.


1 Answers

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.

like image 71
mario.van.zadel Avatar answered Sep 18 '22 14:09

mario.van.zadel