php export data to custom CSV

143 Views Asked by At

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 ;
    }
2

There are 2 best solutions below

3
Gerard de Visser On

You're using space as delimiter. Also some other minor issues in your code.

Try this code instead:

   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){
                $data[] = 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 ;
    }
2
Barmar On

From the documentation:

Note:

If an enclosure character is contained in a field, it will be escaped by doubling it, unless it is immediately preceded by an escape_char.

Since you set $enclosure = " ", any unescaped space character is doubled.

If you don't want it adding enclosure characters, because you're wrapping everything in quotes in the caller, use $enclosure = "".