如何在php中使用fputcsv以csv格式导出数据时处理unicode?

时间:2016-05-31 08:08:58

标签: php csv unicode utf-8

我有以下代码以csv格式导出数据。它在普通文本中工作正常,但如果存在unicode意味着导出的文本难以辨认,即使选择了charset = utf-8,也无法正常工作

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; 
filename=magazine_subscribers'.date('Y-m-d').'.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// output the column headings
fputcsv($output, array('Customer Id', 'Name', 'Company','Subscription Type','Address 1','Postal Code','City','Email','mobile','note','Invoice Receiver','Invoice Email','Invoice address','Invoice city','Invoice Postal Code'));

foreach($subscribers as $subscriber)
{
    fputcsv($output,array($subscriber->customer_id,$subscriber->name,$subscriber->company,$subscriber->subscription_type,$subscriber->address_1,$subscriber->postal_code,$subscriber->city,$subscriber->email,$subscriber->mobile,$subscriber->note,$subscriber->invoice_receiver,$subscriber->invoice_email,$subscriber->invoice_address,$subscriber->invoice_city,$subscriber->invoice_zip));
}

提前致谢

0 个答案:

没有答案