{"record":{"id":"6a6f12b132a8cd6c","repo":"twigphp/Twig","slug":"unable-to-convert-encoding-required-function-iconv-does-not-6a6f12","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":"error","filePath":"src/Runtime/EscaperRuntime.php","lineNumber":335,"sourceCode":"\n            case 'url':\n                return rawurlencode($string);\n\n            default:\n                if (\\array_key_exists($strategy, $this->escapers)) {\n                    return $this->escapers[$strategy]($string, $charset);\n                }\n\n                $validStrategies = implode('\", \"', array_merge(['html', 'js', 'url', 'css', 'html_attr', 'html_attr_relaxed'], array_keys($this->escapers)));\n\n                throw new RuntimeError(\\sprintf('Invalid escaping strategy \"%s\" (valid ones: \"%s\").', $strategy, $validStrategies));\n        }\n    }\n\n    private function convertEncoding(string $string, string $to, string $from)\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","sourceCodeStart":317,"sourceCodeEnd":341,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Runtime/EscaperRuntime.php#L317-L341","documentation":"convertEncoding() throws this when escape() needs to convert the string to UTF-8 (charset differs from 'UTF-8') but the iconv() function is unavailable because neither ext-iconv nor symfony/polyfill-iconv is installed.","triggerScenarios":"Calling escape() with $charset !== 'UTF-8' (e.g. 'ISO-8859-1') on a PHP build without ext-iconv and without symfony/polyfill-iconv in the dependency tree.","commonSituations":"Deploying to a minimal PHP container/Docker image with iconv stripped; a hosting environment with a reduced extension set; composer install without the polyfill.","solutions":["Install/enable the iconv extension (php-iconv, docker-php-ext-install iconv, uncomment extension=iconv in php.ini).","Run: composer require symfony/polyfill-iconv.","Ensure templates run with the default UTF-8 charset so no conversion is attempted.","Verify with php -m | grep iconv after deployment."],"exampleFix":"// before\n # docker image without iconv\n// after\n RUN docker-php-ext-install iconv\n # or\n composer require symfony/polyfill-iconv","handlingStrategy":"fallback","validationCode":"if (!function_exists('iconv') && !extension_loaded('iconv')) {\n    // force UTF-8 charset usage or install polyfill before rendering\n}","typeGuard":"function iconvAvailable(): bool { return function_exists('iconv'); }","tryCatchPattern":"try {\n    $out = $escaper->escape($env, $value, 'html', $charset);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'iconv() does not exist')) {\n        $out = $escaper->escape($env, mb_convert_encoding($value, 'UTF-8', $charset), 'html');\n    } else { throw $e; }\n}","preventionTips":["Add symfony/polyfill-iconv to composer.json as a hard requirement.","Verify required PHP extensions in CI and the deploy image.","Add php -m checks to the Dockerfile entrypoint/healthcheck.","Render with the default UTF-8 charset to avoid conversion entirely."],"tags":["php","iconv","missing-extension","encoding"],"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"}