I am writing a function to export my data to csv. What I want is every data should be in double quotes. It was working fine until I notice a problem:
Output is as follows normal data for example
"Hello";"1232323";
is displaying fine. Only problem comes when there is a space between data
"Hello world"
it displays with one extra space and one space before and after quotations.
could anyone help me how can i remove this extra space, or what I am doing wrong,
protected function outputCSV($data, $delimeter = ';',$enclosure = " ") {
$output = fopen("php://output", "w");
foreach ($data as $row) {
fputcsv($output, $row,$delimeter, $enclosure);
}
fclose($output);
}
public function ExportCsv($id)
{
if(!$id) return false;
/* Headers */
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=result_file.csv");
header("Pragma: no-cache");
header("Expires: 0");
/* get Reiseverlauf Data */
$rf = $this->getReiseVerlaufByDatasetId($id);
if($rf[0]){
foreach ($rf as $rv){
$dataata[] = array('"'.date("d.m.y l", strtotime($rv['date_from'])).'"','"'.date("d.m.y", strtotime($rv['date_to'])).'"','"'.$rv["service"].'"','"'.$rv["location"].'"');
}
}
$this->outputCSV($data, ";" , " " );
exit ;
}
You're using space as delimiter. Also some other minor issues in your code.
Try this code instead: