我需要将数据导出为csv。除货币符号外,一切都在增加。
例如。货币£显示为£
我的项目是一个laravel 4.2应用程序。
$filename = $customer['firstname']."_".date('Y-m-d h:i').".csv";
$file_path = storage_path(). "/transactions"."/".$filename;
$handle = fopen($file_path,"w+");
fputcsv($handle, array('Date', 'Type', 'Ticket', 'Quantity', 'Ticket type', 'Block', 'Event date', 'Paid', 'Recieved'));
foreach($ticket as $row) {
if($row->type == 'purchased')
{
$p_amount = trans('homepage.currencyInUse') .' '.number_format($row->amount, 2).' '.trans('homepage.currencyAfter');
$s_amount = ' ';
}
elseif($row->type == 'sold')
{
$p_amount = ' ';
$s_amount = trans('homepage.currencyInUse').' '.number_format($row->amount, 2).' '.trans('homepage.currencyAfter');
}
fputcsv($handle, array(
date('d/m/Y', strtotime($row->orderDate)),
$row->type,
$row->event->title,
$row->qty,
$row->ticketType,
trans('homepage.Block').':'.$row->ticket['ticketInformation']['loc_block'] ,
date('d/m/Y', strtotime($row->event->datetime)),
$p_amount,
$s_amount,
)
);
}
fclose($handle);
$headers = array(
'Content-Type' => 'text/csv',
);
return Response::download($file_path, $filename, $headers);
此代码用于导出为csv
答案 0 :(得分:4)
您可以使用html_entity_decode()
将HTML实体转换为UTF-8字符:
$currency_use = html_entity_decode(trans('homepage.currencyInUse'), ENT_HTML5, 'utf-8');
$currency_after = html_entity_decode(trans('homepage.currencyAfter'), ENT_HTML5, 'utf-8');
if($row->type == 'purchased')
{
$p_amount = $currency_use.' '.number_format($row->amount, 2).' '.$currency_after;
$s_amount = ' ';
}
elseif($row->type == 'sold')
{
$p_amount = ' ';
$s_amount = $currency_use.' '.number_format($row->amount, 2).' '.$currency_after;
}
以下是一个例子:
$currency = '£';
echo $currency, PHP_EOL;
echo html_entity_decode($currency, ENT_HTML5, 'utf-8'), PHP_EOL;
输出:
£
£