{"record":{"id":"74e193e6e6acd5dd","repo":"symfony/polyfill-mbstring","slug":"argument-1-encoding-must-be-a-valid-encoding-s-given","errorCode":null,"errorMessage":"Argument #1 ($encoding) must be a valid encoding, \"%s\" given","messagePattern":"Argument #1 \\(\\$encoding\\) must be a valid encoding, \"(.+?)\" given","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Mbstring.php","lineNumber":415,"sourceCode":"    public static function mb_internal_encoding($encoding = null)\n    {\n        if (null === $encoding) {\n            return self::$internalEncoding;\n        }\n\n        $normalizedEncoding = self::getEncoding($encoding);\n\n        if ('UTF-8' === $normalizedEncoding || false !== @iconv($normalizedEncoding, $normalizedEncoding, ' ')) {\n            self::$internalEncoding = $normalizedEncoding;\n\n            return true;\n        }\n\n        if (80000 > \\PHP_VERSION_ID) {\n            return false;\n        }\n\n        throw new \\ValueError(\\sprintf('Argument #1 ($encoding) must be a valid encoding, \"%s\" given', $encoding));\n    }\n\n    public static function mb_language($lang = null)\n    {\n        if (null === $lang) {\n            return self::$language;\n        }\n\n        switch ($normalizedLang = strtolower($lang)) {\n            case 'uni':\n            case 'neutral':\n                self::$language = $normalizedLang;\n\n                return true;\n        }\n\n        if (80000 > \\PHP_VERSION_ID) {\n            return false;","sourceCodeStart":397,"sourceCodeEnd":433,"githubUrl":"https://github.com/symfony/polyfill-mbstring/blob/d3d318bad5e7a1bfbd026009c8bfb8d8f99ae6b6/Mbstring.php#L397-L433","documentation":"Mbstring::mb_internal_encoding() throws this ValueError when the requested encoding name is not supported. This polyfill only supports 'UTF-8', and on PHP >= 8.0 an invalid encoding raises a ValueError instead of returning false (which is the pre-8.0 behavior). It is rethrown via mb_internal_encoding and its internal callers (mb_str_split, mb_scrub, mb_str_pad, mb_ucfirst, mb_lcfirst, mb_internal_trim).","triggerScenarios":"Calling mb_internal_encoding('ISO-8859-1') (or any encoding other than 'UTF-8') with $encoding != null when PHP_VERSION_ID >= 80000; indirectly, calling mb_scrub/mb_str_pad/mb_ucfirst/mb_lcfirst/mb_internal_trim with an explicit unsupported $encoding argument.","commonSituations":"Porting legacy code that used ISO-8859-1/Windows-1252 encodings with ext-mbstring to this polyfill; misremembering that the polyfill supports all mbstring encodings when it only supports UTF-8; passing a user-supplied charset from config or HTTP headers without whitelisting UTF-8.","solutions":["Use 'UTF-8' as the encoding — this polyfill only supports UTF-8","Convert your data to UTF-8 (e.g. iconv/mb_convert_encoding via ext-mbstring or a converter) instead of asking for another encoding","Install the native ext-mbstring extension if non-UTF-8 encodings are required","Wrap the call in try/catch (ValueError on PHP 8+, or check the false return value on PHP < 8)"],"exampleFix":"// before\nmb_internal_encoding('ISO-8859-1');\n// after\nmb_internal_encoding('UTF-8'); // polyfill supports UTF-8 only","handlingStrategy":"validation","validationCode":"if ($encoding !== null && !@mb_check_encoding('', $encoding)) {\n    $encoding = 'UTF-8';\n}\nmb_internal_encoding($encoding);","typeGuard":"function isValidEncoding(?string $encoding): bool\n{\n    return $encoding === null || @mb_check_encoding('', $encoding);\n}","tryCatchPattern":"try {\n    mb_internal_encoding($encoding);\n} catch (\\ValueError $e) {\n    mb_internal_encoding('UTF-8'); // polyfill default\n}","preventionTips":["Only use 'UTF-8' with this polyfill","Convert data to UTF-8 at ingestion boundaries","Whitelist encodings from user/config input","Install ext-mbstring when non-UTF-8 encodings are truly needed","Check PHP version when relying on return-value vs exception behavior"],"tags":["php","encoding","mbstring","polyfill","valueerror"],"backgroundTag":"invalid-argument-value","analyzedSha":"d3d318bad5e7a1bfbd026009c8bfb8d8f99ae6b6","analyzedAt":"2026-09-13T19:34:02.044Z","contentChangedAt":"2026-09-13T19:34:02.044Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}