symfony/polyfill-mbstring · error · ValueError

(dynamic: errorFormat passed to assertEncoding, e.g…

Error message

%s (dynamic: errorFormat passed to assertEncoding, e.g. 'mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given')

What it means

Same assertEncoding() validation path as error 6, but on the branch where the encoding check completes without throwing yet still reports the encoding as invalid; the ValueError message is formatted from the caller's $errorFormat, e.g. 'mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given'. Thrown for mb_scrub, mb_str_pad, mb_ucfirst, mb_lcfirst, mb_internal_trim when the $encoding argument names an unsupported encoding.

Solutions

  1. Whitelist the encoding against supported values before calling
  2. Normalize the encoding name (case/aliases) and default to 'UTF-8'
  3. Convert input data to UTF-8 upstream instead of passing other encodings
  4. Wrap calls in try/catch (\ValueError) with a fallback to the default encoding

Example fix

// before
mb_str_pad($s, 10, '.', STR_PAD_RIGHT, $userEncoding);
// after
$enc = @mb_check_encoding('', $userEncoding) ? $userEncoding : 'UTF-8';
mb_str_pad($s, 10, '.', STR_PAD_RIGHT, $enc);
Defensive patterns

Strategy: validation

Validate before calling

$encoding = @mb_check_encoding('', $encoding) ? $encoding : 'UTF-8';
mb_str_pad($string, $length, $padString, $padType, $encoding);

Type guard

function normalizeEncoding(?string $encoding): string
{
    return ($encoding !== null && @mb_check_encoding('', $encoding)) ? $encoding : 'UTF-8';
}

Try / catch

try {
    $padded = mb_str_pad($s, $len, $pad, $padType, $encoding);
} catch (\ValueError $e) {
    $padded = mb_str_pad($s, $len, $pad, $padType, 'UTF-8');
}

Prevention

When it happens

Trigger: Passing an explicit $encoding argument that mb_check_encoding('' ) rejects — e.g. mb_str_pad($s, 10, '.', STR_PAD_RIGHT, 'ASCII-8') or any unsupported name on PHP >= 8.0.

Common situations: Encoding names from config files or environment variables that were never validated; data pipelines passing through encodings inherited from legacy systems; assuming the polyfill supports the full native mbstring encoding list.

Related errors


AI-assisted analysis of symfony/polyfill-mbstring@d3d318bad5 (2026-09-13). Data as JSON: /api/errors/8734aa4b9bce4f9a. Report an issue: GitHub.

Appendix: source

Thrown at Mbstring.php:1132

            return $string;
        }

        return self::iconv('UTF-8', $encoding, $string);
    }

    private static function assertEncoding(string $encoding, string $errorFormat): bool
    {
        try {
            $validEncoding = @self::mb_check_encoding('', $encoding);
        } catch (\ValueError $e) {
            throw new \ValueError(\sprintf($errorFormat, $encoding));
        }

        if (!$validEncoding) {
            if (80000 > \PHP_VERSION_ID) {
                trigger_error(\sprintf($errorFormat, $encoding), \E_USER_WARNING);
            } else {
                throw new \ValueError(\sprintf($errorFormat, $encoding));
            }
        }

        return $validEncoding;
    }
}

View on GitHub (pinned to d3d318bad5)