Generere csv fil i utf-8 format
Hej eksperter.Når jeg generere en csv fil, bliver indholdet ikke gemt i utf-8 format altså æøå ser forkert ud?
Jeg har søgt en del på nettet med det virker ikke bare at lægge en hedder ind som her i
header("Content-Type: text/csv; charset=UTF-8");
Min kode ser sådan her ud:
function query_to_csv($db_conn, $query, $filename, $attachment = false, $headers = true,$campaignid) {
$querykeys = "SELECT * FROM tabel WHERE (Type='1')";
$resultkeys = mysql_query($querykeys, ****) or die(mysql_error());
$headlabelid = array();
$headlabel = array();
while($rowkey = mysql_fetch_array($resultkeys))
{
$headlabelid[] = "tabel".$rowkey['Id'];
$headlabel[] = str_replace(","," ",$rowkey['Question']);
};
if($attachment) {
// send response headers to the browser
header("Content-Type: text/csv; charset=UTF-8");
header( 'Content-Disposition: attachment;filename='.$filename);
$fp = fopen('php://output', 'w');
} else {
$fp = fopen($filename, 'w');
}
$result = mysql_query($query, ****) or die( mysql_error( *** ) );
if($headers) {
// output header row (if at least one row exists)
$row = mysql_fetch_assoc($result);
if($row) {
$intCount = 0;
$newkeyarr = array_keys($row);
foreach ($headlabelid as $value) {
$rewiterows = str_replace($value, $headlabel[$intCount],$newkeyarr);
$newkeyarr = $rewiterows;
$intCount++;
}
fputcsv($fp, $newkeyarr);
// reset pointer back to beginning
mysql_data_seek($result, 0);
}
}
while($row = mysql_fetch_assoc($result)) {
fputcsv($fp, $row);
}
fclose($fp);
}