Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

mb_detect_encoding

Description

Detect character encoding

Syntax

mb_detect_encoding(
    string $string,
    array|string|null $encodings = null,
    bool $strict = false
): string|false

Parameters

string

The string being inspected.

encodings

A list of character encodings to try, in order. The list may be specified as an array of strings, or a single string separated by commas.

If encodings is omitted or null, the current detect_order (set with the mbstring.detect_order configuration option, or mb_detect_order() function) will be used.

strict

Controls the behaviour when string is not valid in any of the listed encodings. If strict is set to false, the closest matching encoding will be returned; if strict is set to true, false will be returned.

The default value for strict can be set with the mbstring.strict_detection configuration option.

Return

The detected character encoding, or false if the string is not valid in any of the listed encodings.

Examples

1 · string

<?

$string = "\xE1\xE9\xF3\xFA";

$return = mb_detect_encoding($string);

echo $return;

?>
ASCII

2 · encodings · array

<?

$string = "\xE1\xE9\xF3\xFA";
$encodings = ['ASCII', 'UTF-8'];

$return = mb_detect_encoding($string, $encodings);

echo $return;

?>
ASCII

3 · encodings · string

<?

$string = "\xE1\xE9\xF3\xFA";
$encodings = 'ASCII, UTF-8';

$return = mb_detect_encoding($string, $encodings);

echo $return;

?>
ASCII

4 · strict · false

<?

$string = "\xE1\xE9\xF3\xFA";
$encodings = ['ASCII', 'UTF-8'];
$strict = false;

$return = mb_detect_encoding($string, $encodings, $strict);

echo $return;

?>
ASCII

5 · strict · true

<?

$string = "\xE1\xE9\xF3\xFA";
$encodings = ['ASCII', 'UTF-8'];
$strict = true;

$return = mb_detect_encoding($string, $encodings, $strict);

var_export($return);

?>
false

6 · first valid

<?

$string = "\xE1\xE9\xF3\xFA";
$encodings = ['UTF-8', 'ASCII'];

$return = mb_detect_encoding($string, $encodings);

echo $return;

?>
UTF-8
HomeMenu