Error: "Input is not proper UTF-8, indicate encoding !" using PHP's simplexml_load_string

Error: "Input is not proper UTF-8, indicate encoding !" using PHP's simplexml_load_string

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

Stefan Bogdanescu

Founder & Senior Architect

Error: "Input is not proper UTF-8, indicate encoding !" using PHP's simplexml_load_string

You are encountering a classic parsing failure where the XML declaration claims UTF-8, but the byte stream doesn't match that promise. The error message parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xED 0x6E 0x2C 0x20 gives you a specific clue: PHP has detected invalid sequences at the very beginning of your input.

In this scenario, the third-party source sends an XML document containing Spanish text like Dublín, which requires UTF-8 encoding to be valid. However, the raw response bytes are inconsistent with the declared <?xml version="1.0" encoding="UTF-8"?> header. This mismatch usually stems from one of two causes: the data was incorrectly encoded (perhaps as ISO-8859-1 or Windows-1252) before being sent, or the source server failed to properly escape characters during a conversion process. Since you cannot modify the third party's code, you must handle the normalization on your end.

Detecting and Correcting Encoding

Before attempting to load the XML with simplexml_load_string, you need to identify the actual encoding of the incoming string. PHP provides built-in functions for this via iconv or mbstring, but a more robust approach involves checking the byte order mark (BOM) or using mb_detect_encoding.

If the third party is sending ISO-8859-1 (Latin-1), which is common in older European systems, you can attempt to detect and re-encode the string before parsing. Here is a practical pattern:

$xmlString = $rawResponse; // The string returned by cURL or file_get_contents

// Attempt to detect the current encoding
$detectedEncoding = mb_detect_encoding($xmlString, ['UTF-8', 'ISO-8859-1', 'Windows-1252'], true);

if ($detectedEncoding === false) {
    // Fallback detection or assume ISO-8859-1 if detection fails for Latin characters
    $detectedEncoding = 'ISO-8859-1';
}

// Normalize the string to UTF-8 before parsing
$normalizedXml = iconv($detectedEncoding, 'UTF-8//IGNORE', $xmlString);

if ($normalizedXml === false) {
    throw new Exception('Failed to convert encoding. Check source data integrity.');
}

$simplexml = simplexml_load_string($normalizedXml);

The //IGNORE suffix in the iconv call is critical here. It tells PHP to drop any characters that cannot be mapped to UTF-8 rather than failing immediately. This often salvages documents where the third party mixed encoding schemes or corrupted specific bytes. Without this flag, iconv might return false, causing your script to crash before you even reach the XML parser.

The BOM Problem

Another frequent culprit in this type of error is the Byte Order Mark (BOM). Some systems prepend a BOM sequence (EF BB BF) to UTF-8 files automatically. While modern parsers usually handle this gracefully, older configurations or specific library versions might choke on it if they strictly interpret the encoding="UTF-8" declaration as "no BOM allowed."

If you suspect a BOM is present, stripping it before parsing is a safe preprocessing step:

// Strip UTF-8 BOM if present
if (substr($normalizedXml, 0, 3) === pack('CCC', 0xEF, 0xBB, 0xBF)) {
    $normalizedXml = substr($normalizedXml, 3);
}

This ensures that the XML parser receives a clean stream starting immediately with <, regardless of whether the sender included the BOM or not.

Handling Mixed Content and Limitations

It is important to recognize that fixing encoding on the fly is a workaround, not a cure-all. If the third party's data contains invalid UTF-8 sequences that cannot be mapped (e.g., replacement characters �), the //IGNORE flag will silently remove them. You must verify the resulting XML structure to ensure no critical data was lost during conversion.

If you are building an application where data integrity is paramount, relying on third-party sources to provide malformed XML is a fragile architecture. However, when that option is unavailable, preprocessing with iconv and stripping BOMs provides a reliable safety net.

For developers working within the Laravel ecosystem, ensuring consistent encoding handling is often best done at the boundary layer. You might consider creating a service class dedicated to XML normalization that wraps this logic, keeping your controllers clean of encoding-specific hacks. Tools like https://laravelcompany.com often discuss best practices for managing external data sources in modern stacks, emphasizing the importance of validating incoming payloads before heavy processing begins.

Our network