PHP generated csv mac garbled

Source: Internet
Author: User
Tags php source code
This article mainly introduces the PHP generated CSV mac garbled, has a certain reference value, now share to everyone, the need for friends can refer to


<?php     $file _name = Date (' Ymd ', Time ()). '. csv ';  Set the file name     header (' Content-type:application/vnd.ms-excel '); header (' Content-disposition:attachment;filename= '). $file _name); header (' cache-control:max-age=0 '); $fp = fopen (' php://output ', ' a '); $row = [' name ' = ' test ', ' email ' = 111111, ' mobile ' = 22222, ' weixinid ' = ',];fwrite ($fp, "\XEF\XBB\XBF"); Fputcsv ($fp, $row);


PHP export CSV file garbled problem solving method



Before you say this question, what is a CSV file first? Comma Separator Value (comma separated value) is also. Intermediate files that are often used for data conversion exist, such as exporting data from MySQL to CSV and importing CSV into SQL Server. In Linux under the PHP script from the MySQL database to export the table data to CSV, using UTF-8 encoding export CSV file, open the inside of the Chinese became garbled (Windows CSV file by default associated with Microsoft Excel), Use notepad++ or Word to open the normal, but the layout is very messy. Reason: BOM trouble, Microsoft caused the curse.

What is a BOM? The Byte order mark (bit-order mark) is also.

To identify Unicode files, Microsoft recommends that all Unicode files begin with the ZERO WIDTH nobreak space character. This is known as a "signature" or "byte order mark (Byte-order Mark,bom)" To identify the encoding and byte order (Big-endian or Little-endian) used in the file, as described in the following table.

bytes Encoding Form

utf-32, Big-endian
ff FE utf-32, Little-endian
fe FF utf-16, Big-endian
ff FE utf-16, Little-endian
ef BB BF utf-8


The BOM is not used in Unix-like systems because it destroys the syntax conventions of existing ASCII files.

Implementation code IF

Note: When writing a CSV file, make sure that the PHP source code is utf-8, and that there is no BOM, and that no content is output.




Excel reads the CSV by reading the BOM on the header of the file to identify the encoding, if the file header does not have BOM information, it is read by default in Unicode encoding. (This BOM is Microsoft's own definition of a file header contract, as the name implies, stored in the file header, stored content is to identify the file encoding information.) )

The platform we generate CSV does not necessarily follow Microsoft's BOM protocol, which causes garbled problems if the output of a non-Unicode encoded CSV file (for example, Utf-8) and no BOM information is generated, and Excel automatically reads in Unicode encoding.

Mastered this, believe garbled has not stopped our pace of progress: just a non-Unicode encoded CSV file, with a text editor (recommended notepad++) open and converted to a BOM with the encoding form (the specific encoding method arbitrary), problem solving.

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.