Home > Backend Development > PHP Problem > What to do if php exports csv with garbled characters

What to do if php exports csv with garbled characters

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

The solution to the garbled CSV exported by php is to output the BOM header at the beginning of the file to tell Windows the encoding method of the CSV file, so that Excel can use the correct encoding when opening the CSV.

What to do if php exports csv with garbled characters

Solution to garbled characters when exporting CSV files from PHP

When working on a project and encountering the use of foreign languages , 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 method of text files

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

For more related knowledge, please visit PHP Chinese website!

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

Related labels:
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