Current Location: Home> Function Categories> mb_detect_encoding

mb_detect_encoding

Detect character encoding
Name:mb_detect_encoding
Category:Multi-byte string
Programming Language:php
One-line Description:Detect character encoding of strings

Function name: mb_detect_encoding()

Applicable version: PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8

Function description: mb_detect_encoding() function is used to detect character encoding of a string.

Usage: string mb_detect_encoding ( string $str [, mixed $encoding_list = mb_detect_order() [, bool $strict = FALSE ]] )

parameter:

  • $str: The string to detect the encoded.
  • $encoding_list (optional): A comma-separated character encoding list that specifies the encoding to be detected. If this parameter is not provided, the internally set detection sequence is used.
  • $strict (optional): A boolean value indicating whether strict mode is enabled. If strict mode is enabled, only exact matching encoding is returned.

Return value: If a matching encoding is found, the encoding name is returned, otherwise FALSE is returned.

Example 1:

 $str = "Hello, world!"; $encoding = mb_detect_encoding($str); echo "编码为:" . $encoding;

Output: Encoding as: ASCII

Example 2:

 $str = "你好,世界!"; $encoding = mb_detect_encoding($str, "UTF-8,GB2312"); echo "编码为:" . $encoding;

Output: Encoding as: UTF-8

Example 3:

 $str = "こんにちは、世界!"; $encoding = mb_detect_encoding($str, "SJIS,EUC-JP,UTF-8"); echo "编码为:" . $encoding;

Output: Encoding as: UTF-8

Notes:

  • Before using this function, you need to make sure that the mbstring extension is installed and enabled.
  • If the $encoding_list parameter is not provided, the detection order returned by the mb_detect_order() function is used.
  • If the $strict parameter is set to TRUE, only the exact matching encoding will be returned. Otherwise, the most likely encoding will be returned.
  • If the detected encoding is UTF-8, it may be UTF-8 or ASCII encoding, further verification is required.
  • This function is only used to detect the encoding of a string and does not encode and convert the string.
Similar Functions
Popular Articles