iconv - Detected an illegal character in input string

iconv - Detected an illegal character in input string

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

Stefan Bogdanescu

Founder & Senior Architect

iconv - Detected an illegal character in input string

When you encounter the iconv() warning "Detected an illegal character in input string," it usually signals a mismatch between your expectations of how PHP handles strings and the reality of the data on disk. The specific scenario here involves converting XML content from a Windows code page (windows-1252) to UTF-8. While the logic iconv($matches[1], 'utf-8', $xml) looks straightforward, the error implies that $xml contains bytes that are valid within the windows-1252 character set but cannot be mapped to a valid Unicode code point during the conversion process.

This often happens when the input string is not actually pure windows-1252. In many legacy systems, files might claim to be encoded in one way while containing bytes that belong to another, or they might contain multi-byte sequences that have been corrupted or truncated. For instance, if a file was saved as UTF-8 but the header claims windows-1252, iconv will attempt to interpret those high-bit bytes using the Windows table and fail when it hits a sequence that doesn't map cleanly.

The debug output shows the source encoding is identified as windows-1252. This code page is an extension of ISO-8859-1, adding specific characters like the smart quote (») at byte 0x9E. If your input string contains a byte sequence that iconv interprets as part of a valid windows-1252 character but results in an undefined or unmapped character when converting to UTF-8, PHP throws this warning.

A common pitfall is assuming that the file encoding declaration within the XML header matches the actual bytes. If the string contains characters outside the range of 0x00 to 0xFF that are valid in windows-1252 but invalid in the target UTF-8 stream without proper mapping, the conversion fails. Another frequent cause is handling binary data or null bytes incorrectly. The iconv() function treats the input as a string; if there are internal control characters or broken multi-byte sequences, it will flag them as illegal.

To resolve this, you must ensure that the $xml variable contains strictly valid input for the specified source encoding before calling iconv. You can preprocess the string to strip out problematic characters or replace them with their standard equivalents. For example, replacing smart quotes found in windows-1252 with standard ASCII quotes before conversion can prevent the error:

// Preprocess to handle specific Windows-1252 quirks if necessary
$xml = str_replace(['\x9E', '\xA0'], ['"', ' '], $xml); 
// Then proceed with conversion
$xml = iconv('windows-1252', 'utf-8//IGNORE', $xml);

Using the //IGNORE or //TRANSLIT suffix in the destination charset is a critical trade-off. //IGNORE drops invalid characters, which might result in data loss but ensures the script runs. //TRANSLIT attempts to approximate the character with a similar ASCII one (like replacing é with e). The choice depends on whether data integrity or script stability is the priority. If you are processing user-generated content where missing a single quote is catastrophic, //IGNORE might be too aggressive; however, if it's internal metadata where minor corruption is acceptable, dropping the bad bytes is safer than crashing.

Another approach is to validate the input encoding before conversion. You can use mb_detect_encoding() or check against known patterns to ensure the string isn't a malformed UTF-8 sequence masquerading as another encoding. If the file is genuinely mixed encoding, a simple string replace of the declaration in the XML header is insufficient; you must actually fix the bytes.

In the Laravel ecosystem, handling this often requires careful attention to how strings are passed between services. Ensure that request()->input() or similar methods aren't already decoding the stream incorrectly before you reach your custom logic. If you are reading from a file directly using file_get_contents(), verify that the file isn't being read with BOM markers that confuse the encoding detection.

Ultimately, the error stems from the byte sequence in $xml violating the rules of the source charset when mapped to UTF-8. By preprocessing the string to normalize known anomalies or adjusting the iconv flags to handle invalid sequences gracefully, you can prevent the script from halting while managing the potential loss or alteration of data appropriately.

Our network