我正在編寫一個 PHP 腳本,該腳本根據一些數據創建 .csv 文件。不幸的是,在 Excel 中變音符號無法正確顯示:L?l變成L√?l(這只是在 Excel 中,在 Apple 的 Numbers、Atom 和 Textedit 中一切看起來都很好)。我已經嘗試了數百個函數來嘗試獲得正確的編碼(請參閱下面的注釋函數)。有人可以告訴我我做錯了什么嗎?$rows = json_decode('[["L?l"]]');foreach($rows as $key_row => $row) { foreach($row as $key_cell => $cell) { // $rows[$key_row][$key_cell] = utf8_decode($cell); // $rows[$key_row][$key_cell] = iconv('UTF-8', 'Windows-1252', $cell); // $rows[$key_row][$key_cell] = mb_convert_encoding($cell, 'UTF-16LE', 'UTF-8'); // iconv('UTF-8', 'Windows-1252', $rows[$key_row][$key_cell]); // mb_convert_encoding($rows[$key_row][$key_cell], 'UTF-16LE', 'UTF-8'); }}$temp = fopen('php://memory', 'w');foreach($rows as $row) { fputcsv($temp, $row, ';'); }fseek($temp, 0);header('Content-Type: application/csv');header('Content-Disposition: attachment; filename="test.csv";');fpassthru($temp);
1 回答

婷婷同學_
TA貢獻1844條經驗 獲得超8個贊
以下轉換UTF-8 BOM有效:
foreach($rows as $key_row => $row) {
foreach($row as $key_cell => $cell) {
$rows[$key_row][$key_cell] = chr(239) . chr(187) . chr(191) . $cell;
}
}
感謝所有參與的人:)
- 1 回答
- 0 關注
- 108 瀏覽
添加回答
舉報
0/150
提交
取消