Solve PHP to generate UTF-8 encoded CSV file with Excel open garbled problem

Source: Internet
Author: User
Solve the PHP generated UTF-8 encoding CSV file with Excel open garbled problem wrote in fact this problem has been encountered a long time ago, it should be not resolved, at that time, the openoffice was opened normally, while the excel file was not opened normally. after that, the code was not resolved. I encountered this problem again. I searched online and found the cause in a java article because the output CSV file does not contain BOM. solve PHP to generate UTF-8 encoded CSV file with Excel open garbled problem

Write

In fact, this problem has been encountered for a long time before and should not be solved. at that time, it was the case that openoffice was opened normally while excel was not opened normally. later, it was not solved, so I had to convert the encoding.

I encountered this problem again. I searched online and found the cause in a java article because the output CSV file does not contain BOM.

What is BOM?

There is a character named "zero width no-break space" in the UCS encoding, and its encoding is FEFF. FFFE does not exist in the UCS, so it should not appear in actual transmission. We recommend that you first transmit the character "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.
The UTF-8 does not need BOM to indicate the byte order, but BOM can be used to indicate the encoding method. The UTF-8 code for 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 will know that this is UTF-8 encoding.
Windows uses BOM to mark the encoding of text files.

So how to output BOM in PHP?

Before all content is output

Print (chr (0xEF). chr (0xBB). chr (0xBF ));

?
 ?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.