Home  >  Article  >  Backend Development  >  How to solve the garbled CSV exported by utf8 encoded PHP?

How to solve the garbled CSV exported by utf8 encoded PHP?

青灯夜游
青灯夜游Original
2020-08-17 10:43:034334browse

Methods to solve garbled characters: 1. Use iconv function to transcode utf8 encoding to GBK, syntax "$str = iconv('utf-8', 'GBK//IGNORE', $str));" ; 2. Output the BOM header before outputting the content, so that Excel can automatically recognize that it is UTF-8.

How to solve the garbled CSV exported by utf8 encoded PHP?

Recommended: "PHP Video Tutorial"

In fact, the requirement is to export data to Excel, which is very Of course, the famous PHPExcel library comes to mind. This library is very powerful, but it takes up a lot of memory. With more than 100,000 data, it is basically difficult to complete in web requests. Therefore, there is still a need to use native PHP to export lightweight csv.

Under normal circumstances, when exporting UTF-8 encoded regular content to csv, it will be garbled when opened with Excel, let alone some non-mainstream characters or emoji expressions. The reason for the garbled code is that under Windows systems, the default Excel is parsed using GBK encoding. Nowadays, there are still encodings that use UTF-8. If so, well-known products from large companies will produce GBK and other encoding versions in order to save a little bit of traffic. Generally, they use UTF-8.

To solve the problem of garbled characters, the approach in PHP is generally to use the iconv function to transcode to GBK, so that there will be no problem when opening Excel by default. But for mainstream characters such as emoji expressions, GBK cannot display them. If the transcoding fails, the characters will be empty. You can add the //IGNORE tag to ignore them, so that they are ignored and other normal characters can be retained.

$str = iconv('utf-8', 'GBK//IGNORE', $str));

This can be said to be a compromise. Can I output UTF-8 directly without transcoding? Doesn't Excel automatically recognize UTF-8 and use UTF-8 to decode and display it? In fact, it is possible, but it requires a BOM header (there is no such thing in the non-windows world). With this BOM header, it can recognize that it is UTF-8. Otherwise, it cannot recognize it and use the default GBK to process it. Naturally It's just gibberish. How to correctly output the so-called BOM header, look at the code:

$fp = fopen('./test_csv.csv', 'a');
fwrite($fp,chr(0xEF).chr(0xBB).chr(0xBF));//输出BOM头
fputcsv($fp, ['标题']);
fputcsv($fp, ['解决乱码']);
fclose($fp);

The above personal test can solve the so-called garbled characters and normal display of non-mainstream characters such as emoji expressions.

The above is the detailed content of How to solve the garbled CSV exported by utf8 encoded PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn