Home > Article > Backend Development > How to solve the problem of garbled csv output 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.
Related recommendations: "php tutorial"
What is BOM
In UCS encoding There is a character called "ZERO WIDTH NO-BREAK SPACE", 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.
So 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.
Before all content is output
print(chr(0xEF).chr(0xBB).chr(0xBF));
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));
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));
The above is the detailed content of How to solve the problem of garbled csv output from php. For more information, please follow other related articles on the PHP Chinese website!