PHP export CSV when data having UTF8 charcters

PHP export CSV when data having UTF8 charcters

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP export CSV when data having UTF8 characters

When exporting data from a MySQL database stored in utf8_bin to a CSV file, displaying multi-byte characters correctly is a common stumbling block. The issue isn't just about telling the browser how to render the text; it's about how the bytes are encoded and decoded during the transfer between your PHP script and the client.

The scenario often looks like this: your web pages display Unicode text perfectly because the browser interprets the HTTP response as UTF-8 based on the Content-Type header or a meta tag. However, when you generate a .csv or .xls file, you might see question marks (???) or garbled characters like सूरà¥à¤¯à¤¾. This garbled output indicates that the browser is interpreting the byte stream using an incorrect encoding, typically Windows-1252 or ISO-8859-1, instead of UTF-8.

The Header Misconception

A frequent attempt to fix this involves setting the header:

header("Content-Type: application/csv; charset=UTF-8");

While this sets the character set for the HTTP response, it is not a silver bullet. Many web browsers and spreadsheet applications, including older versions of Excel, do not strictly adhere to the charset parameter in the MIME type for CSV downloads. Instead, they often rely on the file extension or assume a default encoding based on the locale.

When you set the header but the browser still renders garbled text, it means the bytes being sent are being misinterpreted by the client's decoding logic. In your specific case, seeing सूरà¥à¤¯à¤¾ suggests that UTF-8 multi-byte sequences (like a single character composed of 3 or 4 bytes) are being split and interpreted as individual characters in a single-byte encoding like Windows-1252.

The BOM Solution

The most reliable way to ensure Excel and other spreadsheet applications correctly interpret UTF-8 data is to prepend a Byte Order Mark (BOM) to the beginning of your output stream. The BOM is an invisible Unicode character (U+FEFF) that tells the application, "The following bytes are in UTF-8."

To implement this, you need to echo the BOM before any other content, including headers if you choose to send them first (though headers usually come before body output). In PHP, you can output the BOM using a hex string:

<?php
// Output the UTF-8 BOM
echo "\xEF\xBB\xBF";

// Now output your data
header("Content-Type: text/csv; charset=UTF-8");
header('Content-Disposition: attachment; filename="export.csv"');

// ... rest of your CSV generation logic

Once the BOM is present, modern versions of Excel will automatically detect the encoding and display the characters correctly. This approach works specifically for UTF-8 encoded files. If you are targeting older systems or specific legacy environments that might not support the BOM method, ensure your client application supports UTF-8 explicitly.

Handling the Data Source

Your database column type is utf8_bin. While this stores data as bytes without sorting rules (useful for case-insensitive comparisons), it does not change how the characters are encoded on disk or in the network stream. As long as your PHP strings are handled correctly, the utf8_bin storage format is transparent to the export process.

However, if you encounter issues where the BOM method fails to resolve garbled text, check your PHP configuration. Ensure that your default internal encoding is compatible with UTF-8. You can force this by setting:

mb_internal_encoding('UTF-8');

This ensures that any string manipulation functions (like str_replace or implode) do not assume a legacy encoding like ISO-8859-1.

Exporting to Excel vs. CSV

If your goal is specifically to create an .xls file rather than a CSV, the strategy changes significantly. The BOM trick works well for .csv files because they are plain text. For .xls files, you cannot simply output raw UTF-8 bytes; the format requires specific binary structures. Attempting to force UTF-8 into a standard Excel binary file often results in corruption or incorrect display unless using specific libraries that handle the internal encoding conversion.

If you need to generate an actual .xls file with UTF-8 support, consider using a library like PhpSpreadsheet. These tools abstract away the low-level byte manipulation and handle the encoding internally, allowing you to save cells as UTF-8 strings without manually injecting BOMs or worrying about binary offsets.

For pure CSV exports where you want maximum compatibility without external dependencies, sticking to the BOM method is the standard engineering practice. It bridges the gap between your PHP script's UTF-8 output and the client's expectation of a specific file encoding.

Our network