PHP.nl

mb_detect_encoding

mb_detect_encoding

Detect character encoding

 **mb_detect_encoding** string $string  $encodings bool $strict

Detects the most likely character encoding for
from a list of candidates. string``string

As of PHP 8.1 this function uses heuristics to detect which of the valid text encodings in the specified list is most likely to be correct and may not be in order of provided. encodings

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.

Waarschuwing: > ### The result is not accurate

The name of this function is misleading, it performs "guessing" rather than "detection".

The guesses are far from accurate, and therefore you cannot use this function to accurately detect the correct character encoding.

string The being inspected. string

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

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

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

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

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

Voorbeeld: example

<?php

$str = "\x95\xB6\x8E\x9A\x83\x52\x81\x5B\x83\x68";

// Detect character encoding with current detect_order
var_dump(mb_detect_encoding($str));

// "auto" is expanded according to mbstring.language
var_dump(mb_detect_encoding($str, "auto"));

// Specify "encodings" parameter by list separated by comma
var_dump(mb_detect_encoding($str, "JIS, eucjp-win, sjis-win"));

// Use array to specify "encodings" parameter
$encodings = [
  "ASCII",
  "JIS",
  "EUC-JP"
];
var_dump(mb_detect_encoding($str, $encodings));
?>
string(5) "ASCII"
string(5) "ASCII"
string(8) "SJIS-win"
string(5) "ASCII"

Voorbeeld: Effect of parameter

<?php
// 'áéóú' encoded in ISO-8859-1
$str = "\xE1\xE9\xF3\xFA";

// The string is not valid ASCII or UTF-8, but UTF-8 is considered a closer match
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8'], false));
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8'], true));

// If a valid encoding is found, the strict parameter does not change the result
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8', 'ISO-8859-1'], false));
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8', 'ISO-8859-1'], true));
?>
string(5) "UTF-8"
bool(false)
string(10) "ISO-8859-1"
string(10) "ISO-8859-1"

In some cases, the same sequence of bytes may form a valid string in multiple character encodings, and it is impossible to know which interpretation was intended. For instance, among many others, the byte sequence "\xC4\xA2" could be:

Voorbeeld: Effect of order when multiple encodings match

<?php
$str = "\xC4\xA2";

// The string is valid in all three encodings, but the first one listed may not always be the one returned
var_dump(mb_detect_encoding($str, ['UTF-8']));
var_dump(mb_detect_encoding($str, ['UTF-8', 'ISO-8859-1', 'ISO-8859-5'])); // as of php8.1 this returns ISO-8859-1 instead of UTF-8
var_dump(mb_detect_encoding($str, ['ISO-8859-1', 'ISO-8859-5', 'UTF-8']));
var_dump(mb_detect_encoding($str, ['ISO-8859-5', 'UTF-8', 'ISO-8859-1']));
?>
string(5) "UTF-8"
string(10) "ISO-8859-1"
string(10) "ISO-8859-1"
string(10) "ISO-8859-5"

mb_detect_order