search
HomeBackend DevelopmentPHP ProblemWhat to do if php exports csv with garbled characters

The solution to the garbled CSV exported by php is to output the BOM header at the beginning of the file to tell Windows the encoding method of the CSV file, so that Excel can use the correct encoding when opening the CSV.

What to do if php exports csv with garbled characters

Solution to garbled characters when exporting CSV files from PHP

When working on a project and encountering the use of foreign languages , we will use UTF-8 encoding. However, when exporting a CSV file using PHP, if the written data is in foreign languages ​​such as Japanese and Korean using UTF-8 encoding, garbled characters will appear.

To solve the garbled problem of CSV files generated by PHP, you only need to output the BOM header at the beginning of the file and tell Windows the encoding method of the CSV file, so that Excel can use the correct encoding when opening the CSV.

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 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. 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 method of text files

Before all content is output

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

Several UTF-encoded BOM headers

   define ('UTF32_BIG_ENDIAN_BOM'   ,   chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF));
   define ('UTF32_LITTLE_ENDIAN_BOM',   chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00));
   define ('UTF16_BIG_ENDIAN_BOM' ,   chr(0xFE) . chr(0xFF));
   define ('UTF16_LITTLE_ENDIAN_BOM',   chr(0xFF) . chr(0xFE));
   define ('UTF8_BOM'   ,   chr(0xEF) . chr(0xBB) . chr(0xBF));

Complete code

header('Expires: 0');
     header('Cache-control: private');
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
     header('Content-Description: File Transfer');
     header('Content-Encoding: UTF-8');
     header('Content-type: text/csv; charset=UTF-8');
     header('Content-Disposition: attachment;            filename=Customers_Export.csv');

echo "\xEF\xBB\xBF"; // UTF-8 BOM
// print(chr(0xEF).chr(0xBB).chr(0xBF));

For more related knowledge, please visit PHP Chinese website!

The above is the detailed content of What to do if php exports csv with garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools