Current Location: Home> Latest Articles> How to Control File Encoding to Avoid Garbled Text When Using fputcsv to Save CSV Files

How to Control File Encoding to Avoid Garbled Text When Using fputcsv to Save CSV Files

M66 2025-06-22

1. fputcsv Function Overview

The fputcsv function in PHP is used to write an array of data to a CSV file. It is commonly used for generating and writing CSV files. The basic syntax of fputcsv is as follows:

fputcsv($file, $fields, $delimiter = ',', $enclosure = '"', $escape_char = '\\');

Here, the $file parameter is the file handle, $fields is the array to be written, $delimiter is the separator (default is a comma), $enclosure is the character that wraps around the fields (default is a double quote), and $escape_char is the escape character.

2. Common Garbled Text Issues

When opening CSV files across different operating systems (such as Windows and Linux), garbled text issues may occur. This happens due to the varying default character encodings used by different systems and applications. For instance, Windows typically uses GBK or Windows-1252 encoding, while Linux and macOS usually rely on UTF-8 encoding.

3. Strategies for Controlling Encoding Format

3.1 Using mb_convert_encoding to Convert Encoding

To ensure the file is displayed correctly across various environments, we can use the mb_convert_encoding function to convert the data to a unified encoding format (UTF-8 is usually recommended) before writing it with fputcsv.

// Assume the data is in Chinese
$data = ['Name', 'Age', 'City'];
$data = array_map(function($value) {
    return mb_convert_encoding($value, 'UTF-8', 'auto'); // Automatically detect and convert to UTF-8
}, $data);
<p>$file = fopen('output.csv', 'w');<br>
fputcsv($file, $data);<br>
fclose($file);<br>

This approach ensures that each column in the CSV file is encoded in UTF-8, thereby preventing garbled text issues.

3.2 Adding BOM Header for UTF-8 Encoding Support

Sometimes, UTF-8 encoded files may be incorrectly recognized as ANSI encoded by certain programs (like Excel). To address this issue, you can add a UTF-8 BOM (Byte Order Mark) at the beginning of the file. This ensures the file is correctly displayed in Excel.

$file = fopen('output.csv', 'w');
<p>// Add BOM header<br>
fwrite($file, "\xEF\xBB\xBF");</p>
<p>$data = ['Name', 'Age', 'City'];<br>
fputcsv($file, $data);</p>
<p>fclose($file);<br>

3.3 Changing Excel's Default Encoding

In some cases, you can directly set the encoding Excel uses when opening a CSV file. While this solution may not apply to all scenarios, it provides an alternative for resolving garbled text. You can specify the encoding format in the URL of the CSV file:

$fileUrl = 'http://m66.net/downloads/csv/output.csv';

This method works when the file is downloaded via URL and the user specifies the encoding format during the download.

3.4 Forcing Output to a Specific Encoding Format

Sometimes, you may need to export a CSV file in a specific encoding format (e.g., GBK). You can convert each line of data to the target encoding format using mb_convert_encoding before writing it.

$data = ['Name', 'Age', 'City'];
$data = array_map(function($value) {
    return mb_convert_encoding($value, 'GBK', 'UTF-8'); // Convert from UTF-8 to GBK
}, $data);
<p>$file = fopen('output.csv', 'w');<br>
fputcsv($file, $data);<br>
fclose($file);<br>

This method is suitable for use on Windows systems in China, preventing garbled text caused by Excel's default GBK encoding.

4. Conclusion

When using PHP's fputcsv function, encoding issues are a common challenge. By controlling the file encoding format, you can ensure that files display correctly across different systems and applications, avoiding garbled text. The following methods can be used:

  • Use mb_convert_encoding to convert data to a unified encoding format.

  • Add a UTF-8 BOM header to ensure Excel correctly recognizes the UTF-8 encoding.

  • Select different character encodings, such as GBK, depending on the operating system.

  • Specify the encoding format directly in the URL (e.g., m66.net) to avoid compatibility issues.

By using these techniques, you can avoid garbled text issues when exporting CSV files, enhancing the user experience.