# ForceUTF8 ForceUTF8 is a PHP library that intelligently converts strings to UTF-8 encoding regardless of their original encoding. It handles mixed-encoded strings containing Latin1 (ISO 8859-1), Windows-1252, and UTF-8 characters, automatically detecting and converting them to proper UTF-8 without corrupting already-valid UTF-8 content. The library solves a common problem where applying PHP's native `utf8_encode()` to an already-UTF8 string produces garbled output. ForceUTF8 provides static methods that safely handle encoding conversion, including fixing double-encoded or multiple-encoded UTF-8 strings that appear garbled due to repeated encoding operations. ## Encoding::toUTF8 Converts any string to UTF-8, automatically detecting whether the input is Latin1 (ISO 8859-1), Windows-1252, UTF-8, or a mix of encodings. The function leaves valid UTF-8 characters unchanged while converting non-UTF8 characters appropriately. It also accepts arrays and will recursively convert all string values. ```php "Jos\xe9", // Latin1 "city" => "São Paulo", // UTF-8 "nested" => array( "text" => "Stra\xdfe" // Latin1 (German "Straße") ) ); $converted = Encoding::toUTF8($data); print_r($converted); // Output: Array ( [name] => José [city] => São Paulo [nested] => Array ( [text] => Straße ) ) ``` ## Encoding::fixUTF8 Repairs garbled UTF-8 strings that have been double-encoded or multiple-encoded. This commonly happens when UTF-8 data is mistakenly treated as Latin1 and encoded again. The function iteratively decodes and re-encodes until the string stabilizes, supporting optional iconv flags for handling special characters. ```php