使用 PHP,我将查询结果导出到 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;
}
最佳答案
将 utf8_decode
应用于结果行中的所有元素,因此只需 array_map
:
fputcsv($fp, array_map('utf8_decode',array_values($row)), ',', '"');
关于php - 在导出为 CSV 之前转换数据以保留重音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4126778/