Solution to how to open garbled CSV files generated by PHP with UTF-8 encoding

WBOY
Release: 2016-07-25 08:52:22
Original
1238 people have browsed it
PHP generates a utc-8 encoded csv file, use excel to open it to view the garbled characters, and share the solution for your reference.

openoffice opens normally but excel opens abnormally. The problem can only be solved after converting the encoding.

Later I found out the reason was that there was no BOM in the output CSV file.

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 means that the byte stream is Big-Endian; If FFFE is received, 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.

So how to output BOM in PHP?

Before everything is output: print(chr(0xEF).chr(0xBB).chr(0xBF));



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