PHP export CSV file with Excel Open appears garbled in Chinese

Source: Internet
Author: User

This post consists of:

garbled situation

Write an export CSV file code, you can output the normal

The use CSV and TXT program to open the file is normal, but the use Excel of open files There is a problem with Chinese garbled (this is strange, why is garbled in it Excel ?) )

By looking at the encoding discovery, the exported CSV file is UTF-8 无BOM编码格式 , and we usually use it UTF-8 编码格式 BOM .

After trying to add BOM , the Chinese garbled problem has been solved.

Add BOM to CSV file

Example code:

$file = fopen($export_file_path, ‘w‘);fwrite($file, chr(0xEF).chr(0xBB).chr(0xBF)); // 添加 BOMforeach ($contens as $content) {        fputcsv($file, $content);}fclose($file);

PHP export CSV file with Excel Open appears garbled in Chinese

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: 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.