Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the solution to the garbled code in csv exported by php

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the solution of php exporting csv Chinese garbled code". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Php export csv Chinese garbled solutions: 1, set the header code to change to UTF-8;2, before the output content output BOM header; 3, the UTF-8 to GB2312.

This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer

Php export csv Chinese garbled how to do?

The solution to the garbled code in CSV exported by PHP: UTF-8 to GB2312

I. background

Due to the needs of the project, to export Excel table data, using the fputcsv method to export data encountered Chinese garbled, went to the Internet to find a solution.

1) change the header encoding to UTF-8.

2) output the BOM header before outputting the content

The above two methods are ineffective, I do not know whether my environmental reasons or other reasons, do not go into it for the time being.

II. Solutions

Because the project defaults to UTF-8 encoding and Excel does not support it, it is necessary to convert UTF-8 to GB2312.

[core] override the fpucsv method to add transcoding:

/ * override the fputcsv method and add the transcoding function * @ param $handle * @ param array $fields * @ param string $delimiter * @ param string $enclosure * @ param string $escape_char * / function fputcsv2 ($handle, array $fields, $delimiter = ",", $enclosure ='", $escape_char ="\ ") {foreach ($fields as $k = > $v) {$fields [$k] = iconv (" UTF-8 "," GB2312//IGNORE ", $v) / / convert UTF-8 to GB2312 code} fputcsv ($handle, $fields, $delimiter, $enclosure, $escape_char);}

Examples of use:

Function test () {/ / filename $filename = "order query result". Date ('Y-m-d Hlav iheader'); / / set the output header information header ('Content-Encoding: UTF-8'); header ("Content-Type: text/csv; charset=UTF-8"); header ("Content-Disposition: attachment; filename= {$filename} .csv") $tableHead = array ('#', 'order id',' order number', 'category', 'customer information', 'craftsman information', 'order status', 'construction status', 'payment status', 'order amount', 'order time', 'remarks') / / get handle $output = fopen ('php://output', 'w') or die ("can't open php://output"); / / output header title fputcsv2 ($output, $tableHead); $list = array (); foreach ($list as $item) {fputcsv2 ($output, array_values ($item)) } / / close the handle fclose ($output) or die ("can't close php://output");} "what is the solution to garbled code in csv exported by php"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report