Fix PHP generated UTF-8 encoded CSV file with Excel open garbled problem
Wrote
In fact, this problem has been encountered long ago, should be unresolved, then the situation is OpenOffice open Normal and Excel open not normal, and later did not solve, can only turn the code.
This time again encountered this problem, in the online search, in a Java article found the reason, is because the output of the CSV file does not have a BOM.
What is a BOM?
There is a character called "ZERO WIDTH no-break SPACE" in the UCS encoding, and its encoding is Feff. Fffe is not a character in UCS, so it should not appear in the actual transmission. The UCS specification recommends that the character "ZERO WIDTH no-break SPACE" be transmitted before the byte stream is transmitted. This means that if the recipient receives Feff, the byte stream is Big-endian, and if Fffe is received, it indicates that the byte stream is Little-endian. So the character "ZERO WIDTH no-break SPACE" is also called a BOM.
The UTF-8 does not require a BOM to indicate byte order, but it can be used to indicate the encoding using a BOM. The UTF-8 code for the character "ZERO WIDTH no-break SPACE" is the EF BB BF. So if the receiver receives a byte stream beginning with the EF BB BF, it knows that this is UTF-8 encoded.
Windows uses a BOM to mark the way a text file is encoded.
So how do you output the BOM in PHP?
Before all content is output
Print (Chr (0xEF). chr (0xBB). Chr (0xBF));
?
?