mb_check_encoding()

mb_check_encoding() — Check if strings are valid for the specified encoding.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.4.3, PHP 5 >= 5.1.3, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Checks if the specified byte stream is valid for the specified encoding. If value is of type array, all keys and values are validated recursively. It is useful to prevent so-called "Invalid Encoding Attack".

Signature

mb_check_encoding(array|string|null $value = null, ?string $encoding = null): bool

Parameters

ParameterTypeWhat it takes
$valuearray|string|nullThe byte stream or array to check. If it is omitted, this function checks all the input from the beginning of the request.
$encoding?stringThe expected encoding.

Return value

Returns true on success or false on failure.

Example

The PHP manual ships no example for mb_check_encoding(); the signature and the parameter table above are the contract.

Changelog

VersionChange
8.1.0Calling this function with null as value or without argument is deprecated.
8.0.0value and encoding are nullable now.
8.0.0Now throws a ValueError if encoding is an invalid encoding. Previously, an E_WARNING was emitted and false was returned.
7.2.0This function now also accepts an array as value. Formerly, only strings have been supported.

All PHP 8.5 built-in functions

Reference card built from the PHP manual page mb_check_encoding() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.