special character to csv in php

6.9k Views Asked by At

here is current way i'm doing for generate csv file in php,

$path = $url . 'temp/' .$filename.'.csv';
   fputcsv($fp, $heders, ",");

        foreach ($new_data as $line) {
            fputcsv($fp, $line, ",");
        }
        fclose($fp);

here is problem which i have,

this is data i have in data set,

Brüel & Kjæ, Type 4226, S/N: 2433646 

enter image description here

when generate csv file it's change to this,

Brüel & Kjæ, Type 4226, S/N: 2433646

enter image description here

i need to get exactly same character to csv. please help me to solve this issue :(

2

There are 2 best solutions below

0
On

Put this line of code at the top of the file in php,

header('Content-Type: text/html; charset=UTF-8');

Hope it will may helpful to resolve your issue.

Thanks.

3
On

Try this, set UTF-8 before getting result for both mysqli query and header

1. $mysqli->set_charset("utf8");

and

2.  header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename=' . $filename);
    header("Content-Transfer-Encoding: UTF-8");
    header('Pragma: no-cache');