在导出到 CSV 之前转换数据以保留重音符号

发布于 2024-10-01 04:20:40 字数 773 浏览 0 评论 0原文

我使用 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;
}

Using PHP, I'm exporting results of a query to CSV. My problem comes when the data contains accent; they are not exported correctly and I lose them all in the generated file.

I used the utf8_decode() function to manually convert the headers and it worked perfectly, but I don't know how to use it for the results array.

Anyone can help me out please!?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2024-10-08 04:20:40

utf8_decode 应用于结果行中的所有元素,因此只需 array_map

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

Apply utf8_decode to all elements in result row, so simply array_map:

fputcsv($fp, array_map('utf8_decode',array_values($row)), ',', '"');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文