UTF-8 problems while reading CSV file with fgetcsv

UTF-8 problems while reading CSV file with fgetcsv

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

Stefan Bogdanescu

Founder & Senior Architect

UTF-8 problems while reading CSV file with fgetcsv

You are seeing "Mäx" instead of "Mäx", but your mb_detect_encoding confirms the data is UTF-8, and even a raw file_get_contents read shows the mojibake. This discrepancy immediately points to a specific encoding mismatch: the file you believe is UTF-8 is actually Windows-1252 (or ISO-8859-1) interpreted as Latin-1.

When you open a CSV in Notepad++ and select "UTF-8 without BOM", it displays correctly because Notepad++ understands that the bytes are Latin-1 but presents them as UTF-8 for display purposes, or you may have saved it as Windows-1252 thinking it was UTF-8. However, when PHP reads the file using fopen and fgetcsv, it treats the bytes strictly according to their binary representation.

The core issue lies in how the character encodings map to byte values. In Latin-1 (Windows-1252), the character "ä" is stored as the byte 0xE4. When you open a file containing this byte and read it assuming UTF-8, PHP expects the sequence 0xC3 0xA4 for "ä". Seeing only 0xE4, UTF-8 decoders often fail gracefully or fall back to displaying the raw bytes in their Latin-1 equivalents if no valid UTF-8 sequence is found, resulting in the "ä" artifact.

Here is the breakdown of what happens:

  1. The Byte: The file contains 0xE4 for "ä".
  2. The Assumption: Your PHP script assumes the stream is UTF-8.
  3. The Mismatch: UTF-8 does not have a single-byte character at 0xE4. That position is reserved for continuation bytes in multi-byte sequences.
  4. The Visual Glitch: When you print the byte 0xE4 directly, your browser (which knows it's HTML) interprets that specific byte value as "Ã" in Latin-1. Then, when it tries to render the next byte (0xA4) which represents "¤" in Latin-1, but is actually part of the "ä" sequence, the result looks like "ä".

The solution is not setlocale. The setlocale function changes how PHP formats numbers and dates (e.g., using commas instead of dots for decimals) and handles multi-byte character sets internally. It does not change the encoding of the file stream returned by fopen. Even if you set the locale to 'de_DE.utf8', fopen still reads raw bytes, and fgetcsv parses those bytes as-is. The "Edit" in your source material confirms this: mb_detect_encoding returns UTF-8, but that function is detecting the content assuming a standard encoding mapping, whereas the raw file content is fundamentally different from what a pure UTF-8 stream should look like.

To fix this, you must explicitly tell PHP how to decode the input stream before processing it with fgetcsv. You can use stream_wrapper_register or simply wrap the file pointer with mb_convert_encoding logic if using custom wrappers, but the most direct approach for CSVs is often to detect and convert on read.

However, relying on mb_detect_encoding after reading the whole line into a string is late-stage detection. The bytes are already in memory as Latin-1 values before you run that check. If you want to handle this robustly without changing the file itself (which requires opening it with hex editors or text editors that can "Save As" UTF-8), you should use mb_convert_encoding on the stream itself or the data immediately after reading.

A reliable pattern involves creating a custom wrapper or converting the content of the row immediately after fgetcsv. Here is how you can handle the conversion right after extracting the row:

$handle = fopen("specialchars.csv", "r");
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
    // The data is currently Latin-1 (Windows-1252) but needs to be treated as UTF-8
    // We convert the array elements to ensure they are properly encoded for output.
    foreach ($data as &$value) {
        if (empty($value)) continue;
        
        // mb_convert_encoding attempts to map the current byte sequence 
        // from the source encoding to the target encoding.
        $value = mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1');
    }

    echo "<tr>";
    foreach ($data as $val) {
        echo "<td>" . htmlspecialchars($val) . "</td>";
    }
    echo "</tr>";
}
fclose($handle);

In this snippet, mb_convert_encoding acts as the bridge. It takes the raw bytes (which are valid Latin-1/Windows-1252) and maps them to their correct UTF-8 byte sequences. This transforms the underlying representation so that when you output $val, your browser receives the correct multi-byte sequence for "ä".

There is a trade-off here: modifying the data in memory doesn't fix the file on disk, but it solves the problem for your application without requiring you to re-export the source CSV. If you are using Laravel, ensure your APP_DEFAULT_ENCODING or database connection settings are consistent with this logic if you are persisting this data later. For raw CSV parsing tasks outside of a framework, sticking to explicit conversion based on known source encodings is safer than relying on detection algorithms which can sometimes be ambiguous with mixed scripts.

Our network