2010-11-08 80 views
1

使用PHP,我將查詢結果導出爲CSV。當數據包含口音時,我的問題就來了。它們不能正確導出,並且我將它們全部丟失在生成的文件中。在導出爲CSV之前轉換數據以保留重音

我用utf8_decode()函數來手動轉換頭文件,它的工作完美,但我不知道如何將它用於結果數組。

任何人都可以幫助我!

result = db_query($sql); 
if (!$result) die('Couldn\'t fetch records'); 

$fp = fopen('php://output', 'w'); 
if ($fp && $result) { 
header("Content-type: application/vnd.ms-excel; charset=UTF-8"); 
header('Content-Disposition: attachment; filename="adp_enigmes_data.csv"'); 
header('Pragma: no-cache'); 
header('Expires: 0'); 
fputcsv($fp, $headerTitles); 

while ($row = $result->fetch_array(MYSQLI_NUM)) { 
    // When I use utf8_decode here, I don't get any results, so I have 
     // no idea where to use it! 
     fputcsv($fp, utf8_decode(array_values($row)), ',', '"'); 
} 
die; 
} 

回答

12

應用utf8_decode在結果行的所有元素,所以乾脆array_map

fputcsv($fp, array_map('utf8_decode',array_values($row)), ',', '"'); 
+0

哇人感謝了很多,很好地工作! – 2010-11-08 19:36:31

+0

正是我所需要的。 – dchayka 2015-01-19 19:03:48

相關問題