{"record":{"id":"7d9383275dd41f29","repo":"symfony/polyfill-mbstring","slug":"s-dynamic-errorformat-passed-to-assertencoding-e-g-mb-scrub","errorCode":null,"errorMessage":"%s (dynamic: errorFormat passed to assertEncoding, e.g. 'mb_scrub(): Argument #2 ($encoding) must be a valid encoding, \"%s\" given')","messagePattern":"(.+?) \\(dynamic: errorFormat passed to assertEncoding, e\\.g\\. 'mb_scrub\\(\\): Argument #2 \\(\\$encoding\\) must be a valid encoding, \"(.+?)\" given'\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Mbstring.php","lineNumber":1125,"sourceCode":"        } else {\n            $characters = preg_quote($characters);\n        }\n\n        $string = preg_replace(\\sprintf($regex, $characters), '', $string);\n\n        if (null === $encoding) {\n            return $string;\n        }\n\n        return self::iconv('UTF-8', $encoding, $string);\n    }\n\n    private static function assertEncoding(string $encoding, string $errorFormat): bool\n    {\n        try {\n            $validEncoding = @self::mb_check_encoding('', $encoding);\n        } catch (\\ValueError $e) {\n            throw new \\ValueError(\\sprintf($errorFormat, $encoding));\n        }\n\n        if (!$validEncoding) {\n            if (80000 > \\PHP_VERSION_ID) {\n                trigger_error(\\sprintf($errorFormat, $encoding), \\E_USER_WARNING);\n            } else {\n                throw new \\ValueError(\\sprintf($errorFormat, $encoding));\n            }\n        }\n\n        return $validEncoding;\n    }\n}\n","sourceCodeStart":1107,"sourceCodeEnd":1139,"githubUrl":"https://github.com/symfony/polyfill-mbstring/blob/d3d318bad5e7a1bfbd026009c8bfb8d8f99ae6b6/Mbstring.php#L1107-L1139","documentation":"assertEncoding() is the polyfill's internal validator: it calls mb_check_encoding and, when the encoding is invalid, throws a ValueError whose message is built from the caller-supplied $errorFormat with the encoding interpolated. The exact message depends on the caller, e.g. 'mb_scrub(): Argument #2 ($encoding) must be a valid encoding, \"%s\" given'. This is thrown whenever mb_scrub, mb_str_pad, mb_ucfirst, mb_lcfirst, or mb_internal_trim receives an unsupported encoding.","triggerScenarios":"Calling mb_scrub($s, 'ISO-8859-1'), mb_ucfirst($s, $enc), mb_lcfirst($s, $enc), or mb_internal_trim(...) with any encoding the polyfill doesn't recognize (it only truly supports UTF-8) on PHP >= 8.0; an invalid encoding returned from a getEncoding()-style default.","commonSituations":"Passing legacy encodings used with native ext-mbstring to the polyfill; encoding names built dynamically from user input, DB config, or HTTP headers without validation; typos like 'utf8' vs 'UTF-8' depending on the accepted alias list.","solutions":["Use 'UTF-8' (the polyfill's supported encoding) or a recognized alias accepted by mb_check_encoding","Validate the encoding before calling: in_array($enc, mb_list_encodings(), true) or @mb_check_encoding('', $enc)","Convert data to UTF-8 first rather than requesting another encoding","Catch \\ValueError and fall back to the default encoding"],"exampleFix":"// before\nmb_scrub($text, 'Windows-1252');\n// after\nmb_scrub(mb_convert_encoding($text, 'UTF-8', 'Windows-1252'), 'UTF-8');","handlingStrategy":"validation","validationCode":"if (!@mb_check_encoding('', $encoding)) {\n    $encoding = 'UTF-8';\n}\nmb_scrub($string, $encoding);","typeGuard":"function isSupportedMbEncoding(?string $encoding): bool\n{\n    return $encoding === null || @mb_check_encoding('', $encoding);\n}","tryCatchPattern":"try {\n    $result = mb_scrub($string, $encoding);\n} catch (\\ValueError $e) {\n    $result = mb_scrub($string, 'UTF-8');\n}","preventionTips":["Default all encodings to 'UTF-8' in polyfill-only codebases","Validate encodings from config/HTTP headers before use","Convert to UTF-8 at system boundaries","Pin down the accepted alias list instead of guessing names like 'utf8'"],"tags":["php","encoding","mbstring","polyfill","valueerror"],"backgroundTag":"invalid-encoding","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"}