Backend Development 2 min read

Using mb_detect_order() to Set and Retrieve Character Encoding Detection Order in PHP

This article explains how the PHP mb_detect_order() function configures or retrieves the list of character encodings used for automatic detection, describes its parameters and return values, and provides practical code examples for both string and array inputs.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Using mb_detect_order() to Set and Retrieve Character Encoding Detection Order in PHP

The mb_detect_order() function in PHP's mbstring extension allows developers to set or get the order in which character encodings are automatically detected.

Parameter: $encoding_list can be an array or a comma‑separated string of encoding names (e.g., "UTF-8, ASCII"). If omitted, the function returns the current detection order as an array.

Supported encodings include UTF‑8, UTF‑7, ASCII, EUC‑JP, SJIS, ISO‑8859‑*, among others. Certain encodings such as UTF‑16, UTF‑32, UCS2, and UCS4 cannot be detected and will always fail.

Return value: On success, the function returns TRUE when setting a new order, or an array of encodings when retrieving the current order; it returns FALSE on failure.

Example usage:

<?php /* Set detection order using a comma‑separated list */ mb_detect_order("eucjp-win,sjis-win,UTF-8"); /* Set detection order using an array */ $ary = "ASCII"; mb_detect_order($ary); /* Display the current detection order */ echo implode(", ", mb_detect_order()); ?>

backendcharacter encodingmbstringmb_detect_order
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.