Reputation: 1604
I have below code in my script.
$file= fopen("./test.csv","x+");
$proj_lists="name,age,address,city name,country name\n";
fputcsv($file,explode(',',$proj_lists),',',' ');
fclose($file);
It creates a csv file with the given values. My problem here is it creates extra space with double word. Example "country name" is written as "country name"(2 spaces in this word).
How to resolve this.
Upvotes: 1
Views: 1767
Reputation: 173562
My problem here is it creates extra space with double word
That's because you chose to use a space as the string enclosure character (4th argument, default value is a double quote). Here's the first line of the output:
name,age,address, city name , country name
^ ^^ ^ ^ ^^
e ee e e ee
Where e
is the enclosure, and ee
is the escaped enclosure, which is the escape character followed by the enclosure character (which happens to be part of the data you're trying to output).
It's best to simply use the defaults that come with fputcsv
; that is, call the function with only two arguments.
fputcsv($file, explode(',', $proj_lists));
Which will output what most spreadsheet applications can read:
name,age,address,"city""name","country name
"
Upvotes: 1
Reputation: 72299
Actually you need to work with fputs()
like below:-
<?php
$file= fopen("./test.csv","x+");
$proj_lists=explode(',',"name,age,address,city name,country name");
fputs($file,implode($proj_lists, ','));
fclose($file);
?>
Output:- name,age,address,city name,country name
Upvotes: 0