Reputation: 115
I have a csv file that I read and save as an output file using the following code :
CSV content :
10038,"Service, Inc.",loten,[email protected],9951,xxx,6321
10041,Roadsi deInc.,Kgel,[email protected],1101,xxx,7967
10042,Rang LLC,Resgers,[email protected],2073,4611,xxx
<?php
$csv_filename = "output.csv";
$fp = fopen($csv_filename, "w");
if (($handle = fopen(__DIR__.'/test.csv', "r")) !== FALSE && $fp) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data_row = array(
$data[0],
$data[1],
$data[2],
$data[3],
$data[4],
$data[5]
);
fputcsv($fp, $data_row, "|");
}
}
fclose($handle);
fclose($fp);
the results I get is like the folowing :
10038|"Service, Inc."|loten|[email protected]|9951|xxx
10041|"Roadsi deInc."|Kgel|[email protected]|1101|xxx
10042|"Rang LLC"|Resgers|[email protected]|2073|4611
how can I remove the quotes that the fputcsv function added when there is a space in a string and how can I add a 0d0a ending to each line of the csv output?
Upvotes: 0
Views: 403
Reputation: 144
The quotes are added by fputcsv, try fputs and join:
<?php
$csv_filename = "output.csv";
$fp = fopen($csv_filename, "w");
if (($handle = fopen(__DIR__.'/test.csv', "r")) !== FALSE && $fp) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data_row = array(
$data[0],
$data[1],
$data[2],
$data[3],
$data[4],
$data[5]
);
fputs($fp, join("|", $data_row)."\r\n";
}
}
fclose($handle);
fclose($fp);
Upvotes: 2