php fputcsv use semicolon separator in CSV

北城余情 提交于 2019-12-21 20:53:14

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/32309554/php-fputcsv-use-semicolon-separator-in-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!