在某些脚本中,我看到它们省略了?>为脚本编写结束标记.为什么我应该这样做?
(我敢肯定他们没有忘记它.)
我在导出带有中文字符的 UTF-8 CSV 时遇到了困难,在 Windows Excel 上显示乱码。我正在使用 PHP,并且已经添加了 BOM 字节标记并尝试了编码,但根本没有运气。
它们在 Notepad++、Google Spreadsheet 甚至 Mac Numbers 上都可以正常打开。但 Excel 上不行,这是客户的要求。使用 Notepad++ 打开时,编码显示为 UTF-8。如果我手动将其更改为 UTF-8 并保存,该文件在 Excel 上可以正常打开。
似乎 BOM 字节标记没有保存在输出中,因为 Notepad++ 总是将其检测为没有 BOM 的 UTF-8。
此外,CSV 不会保存在服务器上。数据从DB中检索,然后直接导出。
这是我的代码:
// Setup headers
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-disposition: filename=".$filename.".csv");
header("Pragma: no-cache");
// First Method
$fp = fopen('php://output', 'w');
// Add BOM to fix UTF-8 in Excel, but doesn't work
fputs($fp, chr(0xEF) . chr(0xBB) . chr(0xBF) );
if ($fp) …Run Code Online (Sandbox Code Playgroud)