What to do if php outputs csv garbled characters

藏色散人
Release: 2023-03-08 11:22:01
Original
2227 people have browsed it

The solution to php output csv garbled code: first open the corresponding code file; then output the BOM header at the beginning of the file, and tell windows the encoding method of the CSV file; finally let Excel use the correct encoding when opening the CSV. Can.

What to do if php outputs csv garbled characters

#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

Solution to garbled characters when exporting CSV files from PHP

When we encounter the use of foreign languages ​​when doing projects, we will use UTF-8 encoding. However, when exporting a CSV file using PHP, if the written data is in foreign languages ​​such as Japanese and Korean using UTF-8 encoding, garbled characters will appear.

To solve the garbled problem of CSV files generated by PHP, you only need to output the BOM header at the beginning of the file and tell Windows the encoding method of the CSV file, so that Excel can use the correct encoding when opening the CSV.

What is BOM

There is a character called "ZERO WIDTH NO-BREAK SPACE" in UCS encoding, and its encoding is FEFF. FFFE is a character that does not exist in UCS, so it should not appear in actual transmission. The UCS specification recommends that we transmit the characters "ZERO WIDTH NO-BREAK SPACE" before transmitting the byte stream. In this way, if the receiver receives FEFF, it indicates that the byte stream is Big-Endian; if it receives FFFE, it indicates that the byte stream is Little-Endian. Therefore

The character "ZERO WIDTH NO-BREAK SPACE" is also called BOM. UTF-8 does not require a BOM to indicate the byte order, but can use the BOM to indicate the encoding method. The UTF-8 encoding of the character "ZERO WIDTH NO-BREAK SPACE" is EF BB BF. So if the receiver receives a byte stream starting with EF BB BF, it knows that it is UTF-8 encoded. Windows uses BOM to mark the encoding of text files.

[Recommended learning: "PHP Video Tutorial"]

Before all content is output

print(chr(0xEF).chr(0xBB).chr(0xBF));
Copy after login

Several UTF-encoded BOM headers

  define ('UTF32_BIG_ENDIAN_BOM'   ,   chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF));
   define ('UTF32_LITTLE_ENDIAN_BOM',   chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00));
   define ('UTF16_BIG_ENDIAN_BOM' ,   chr(0xFE) . chr(0xFF));
   define ('UTF16_LITTLE_ENDIAN_BOM',   chr(0xFF) . chr(0xFE));
   define ('UTF8_BOM'   ,   chr(0xEF) . chr(0xBB) . chr(0xBF));
Copy after login

Complete code

header('Expires: 0');
     header('Cache-control: private');
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
     header('Content-Description: File Transfer');
     header('Content-Encoding: UTF-8');
     header('Content-type: text/csv; charset=UTF-8');
     header('Content-Disposition: attachment;            filename=Customers_Export.csv');

echo "\xEF\xBB\xBF"; // UTF-8 BOM
// print(chr(0xEF).chr(0xBB).chr(0xBF));
Copy after login

The above is the detailed content of What to do if php outputs csv garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template