PHP流CSV总是添加UTF-8 BOM [英] PHP Streaming CSV always adds UTF-8 BOM

查看:932
本文介绍了PHP流CSV总是添加UTF-8 BOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code得到一个报告线作为一个数组,并使用fputcsv它等转换成CSV。除了一个事实,一切是伟大的工作,无论我用的字符集,它是在文件的开头放一个UTF-8 BOM。这是非常讨厌的,因为A)我指定ISO和B)我们有很多使用的工具,展示了UTF-8 BOM作为垃圾字符的用户。

我甚至试过把结果写入字符串,剥UTF-8 BOM,然后echo'ing出来,并仍然得到它。有没有可能是问题所在与Apache?
如果我改变而fopen到本地文件写入它只是罚款没有UTF-8 BOM。

 标题(内容类型:文本/ CSV;字符集= ISO-8859-1);
标题(缓存控制:无店面,无缓存);
标题(内容处置:附件;文件名= \\report.csv \\);$ outstream =的fopen(PHP://输出,W);为($ I = 0; $ I< $报告 - > rowCount等; $ I ++){
    fputcsv($ outstream,$报告 - > getTaxMatrixLineValues​​($ I),',','');
}
FCLOSE($ outstream);出口;


解决方案

我的猜测是,你的PHP源$ C ​​$ C文件有一个BOM,并且启用了PHP的输出缓冲。

The following code gets a 'report line' as an array and uses fputcsv to tranform it into CSV. Everything is working great except for the fact that regardless of the charset I use, it is putting a UTF-8 bom at the beginning of the file. This is exceptionally annoying because A) I am specifying iso and B) We have lots of users using tools that show the UTF-8 bom as characters of garbage.

I have even tried writing the results to a string, stripping the UTF-8 BOM and then echo'ing it out and still get it. Is it possible that the issue resides with Apache? If I change the fopen to a local file it writes it just fine without the UTF-8 BOM.

header("Content-type: text/csv; charset=iso-8859-1");
header("Cache-Control: no-store, no-cache");
header("Content-Disposition: attachment; filename=\"report.csv\"");

$outstream = fopen("php://output",'w');

for($i = 0; $i < $report->rowCount; $i++) {
    fputcsv($outstream, $report->getTaxMatrixLineValues($i), ',', '"');
}
fclose($outstream);

exit;

解决方案

My guess would be that your php source code file has a BOM, and you have php's output buffering enabled.

这篇关于PHP流CSV总是添加UTF-8 BOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆