PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8
mb_detect_encoding - Detect character encoding

     [array|string|null$encodings = null],
     [bool$strict = false]
): string|false

Detects the most likely character encoding for string string from an ordered list of candidates.

Automatic detection of the intended character encoding can never be entirely reliable; without some additional information, it is similar to decoding an encrypted string without the key. It is always preferable to use an indication of character encoding stored or transmitted with the data, such as a "Content-Type" HTTP header.

This function is most useful with multibyte encodings, where not all sequences of bytes form a valid string. If the input string contains such a sequence, that encoding will be rejected, and the next encoding checked.



The string being inspected.


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.


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 Values

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

Related Functions

Example of mb_detect_encoding

Show all examples for mb_detect_encoding

PHP Version: