{"record":{"id":"9be2b02e5ed75e65","repo":"twigphp/Twig","slug":"unable-to-convert-encoding-required-function-iconv-does-not","errorCode":null,"errorMessage":"Unable to convert encoding: required function iconv() does not exist. You should install ext-iconv or symfony/polyfill-iconv.","messagePattern":"Unable to convert encoding: required function iconv\\(\\) does not exist\\. You should install ext-iconv or symfony/polyfill-iconv\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"critical","filePath":"src/Extension/CoreExtension.php","lineNumber":1271,"sourceCode":"     *\n     * @internal\n     */\n    public static function spaceless($content): string\n    {\n        return trim(preg_replace('/>\\s+</', '><', $content ?? ''));\n    }\n\n    /**\n     * @param string|null $string\n     * @param string      $to\n     * @param string      $from\n     *\n     * @internal\n     */\n    public static function convertEncoding($string, $to, $from): string\n    {\n        if (!\\function_exists('iconv')) {\n            throw new RuntimeError('Unable to convert encoding: required function iconv() does not exist. You should install ext-iconv or symfony/polyfill-iconv.');\n        }\n\n        return iconv($from, $to, $string ?? '');\n    }\n\n    /**\n     * Returns the length of a variable.\n     *\n     * @param mixed $thing A variable\n     *\n     * @internal\n     */\n    public static function length(string $charset, $thing): int\n    {\n        if (null === $thing) {\n            return 0;\n        }\n","sourceCodeStart":1253,"sourceCodeEnd":1289,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L1253-L1289","documentation":"CoreExtension::convertEncoding relies on PHP's iconv() function to convert string encodings for filters like `random`, `reverse`, and `shuffle`. If the iconv extension (or its polyfill) is not loaded, the function cannot work and throws this RuntimeError up front instead of failing with an undefined-function fatal error.","triggerScenarios":"Rendering a Twig template that calls the `reverse`, `shuffle`, or `random` filter on a multibyte string, or calling CoreExtension::convertEncoding directly, on a PHP installation where function_exists('iconv') is false — i.e. ext-iconv not compiled/loaded and symfony/polyfill-iconv not installed.","commonSituations":"Docker/PHP images built with --disable-iconv or without the iconv extension; minimal shared-hosting PHP builds; missing `extension=iconv` in php.ini (CLI vs web-server SAPI differences); composer install skipped polyfills.","solutions":["Install/enable ext-iconv: add `extension=iconv` to php.ini (check `php -m | grep iconv`)","Or require the polyfill: `composer require symfony/polyfill-iconv`","Rebuild the PHP Docker image including libiconv / the iconv extension","Verify in the same SAPI that renders templates (`php -r \"var_dump(function_exists('iconv'));\"`), since CLI and FPM configs can differ"],"exampleFix":"// before: composer.json without iconv support\n{}\n// after\ncomposer require symfony/polyfill-iconv\n// and/or php.ini\nextension=iconv","handlingStrategy":"fallback","validationCode":"if (!\\function_exists('iconv')) {\n    throw new \\RuntimeException('ext-iconv or symfony/polyfill-iconv is required');\n}","typeGuard":null,"tryCatchPattern":"try {\n    return $twig->render($template, $vars);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'iconv()')) {\n        // degrade: avoid reverse/shuffle filters or use mb_* based custom filters\n    } else { throw $e; }\n}","preventionTips":["Add ext-iconv to production and CI PHP images","composer require symfony/polyfill-iconv as a safety net","Assert function_exists('iconv') in a boot/health check","Verify extension availability in the same SAPI (CLI vs FPM) used for rendering"],"tags":["php","missing-extension","iconv","environment","twig"],"backgroundTag":"missing-optional-dependency","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}