Current Location: Home> Latest Articles> How to Set Character Encoding Detection Order Using PHP mb_detect_order() Function

How to Set Character Encoding Detection Order Using PHP mb_detect_order() Function

gitbox 2025-06-12

What is the mb_detect_order() Function?

In PHP, the mb_detect_order() function is useful for handling character encoding issues in multilingual environments. It is a built-in PHP function that detects the order of string encoding. By setting the encoding detection order, you can improve the accuracy of encoding detection, thus effectively solving character encoding problems.

Syntax of mb_detect_order() Function

The mb_detect_order() function accepts a parameter $encoding_list, which is an array of encoding names that specifies the order in which PHP detects string encoding. When no parameter is passed, the function returns the current encoding detection order.


mixed mb_detect_order([mixed $encoding_list])

Usage Example of mb_detect_order() Function

Example 1: Get the Current Encoding Detection Order

The following code will output the current encoding detection order used by PHP:


$encoding_list = mb_detect_order();
print_r($encoding_list);

Sample output might look like this:


Array
(
    [0] => ASCII
    [1] => UTF-8
    [2] => GB2312
    [3] => GBK
    [4] => BIG5
    [5] => JIS
)

As you can see from the output, PHP will first attempt to detect the encoding using ASCII, followed by UTF-8, GB2312, GBK, BIG5, and JIS encodings.

Example 2: Set the Encoding Detection Order

If you wish to customize the encoding detection order, you can use the following code:


$encoding_list = array("UTF-8", "GBK", "GB2312", "BIG5");
mb_detect_order($encoding_list);

This code sets the encoding detection order to UTF-8, GBK, GB2312, and BIG5.

Why Set the Encoding Detection Order?

In multilingual or international development, character encoding issues are common. When users input garbled characters on a website, we need to accurately determine the encoding type to correctly interpret the input. This is where PHP's character encoding detection functions come into play.

The mb_detect_encoding() function relies on the encoding detection order set by mb_detect_order(). If the detection order is not set, the function defaults to a built-in encoding sequence. However, the built-in sequence does not include all encoding types, which may lead to inaccurate or incorrect detection. By setting the encoding detection order, PHP can perform more accurate checks, avoiding detection errors.

How to Set the Encoding Detection Order?

When setting the detection order, it's important to understand some basic character encoding types:

  • GBK Encoding: Used for Chinese operating systems, supports Simplified and Traditional Chinese.
  • GB2312 Encoding: Used in China, supports Simplified Chinese.
  • UTF-8 Encoding: A variable-length encoding that can represent all characters in the world.
  • BIG5 Encoding: Used in Taiwan, supports Traditional Chinese.
  • JIS Encoding: Used in Japan, supports Japanese characters.
  • ASCII Encoding: A 7-bit encoding that represents English characters only.

Different character encoding formats have different ways of encoding characters. By setting mb_detect_order(), PHP will attempt to detect the string encoding in the order specified.

Summary

In this article, you have learned how to use PHP's mb_detect_order() function to set the character encoding detection order. Mastering this function can help developers resolve encoding issues in multilingual environments, prevent garbled characters, and improve the user experience.