mb_convert_encoding()
mb_convert_encoding() — Convert a string from one character encoding to another.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Converts string from from_encoding, or the current internal encoding, to to_encoding. If string is an array, all its string values will be converted recursively.
Signature
mb_convert_encoding(array|string $string, string $to_encoding, array|string|null $from_encoding = null): array|string|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $string | array|string | The string or array to be converted. |
| $to_encoding | string | The desired encoding of the result. |
| $from_encoding | array|string|null | The current encoding used to interpret string. Multiple encodings may be specified as an array or comma separated list, in which case the correct encoding will be guessed using the same algorithm as mb_detect_encoding(). |
Return value
The encoded string or array on success, or false on failure.
Example
A runnable snippet:
/* Convert internal character encoding to SJIS */ $str = mb_convert_encoding($str, "SJIS"); /* Convert EUC-JP to UTF-7 */ $str = mb_convert_encoding($str, "UTF-7", "EUC-JP"); /* Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE */ $str = mb_convert_encoding($str, "UCS-2LE", "JIS, eucjp-win, sjis-win"); /* If mbstring.language is "Japanese", "auto" is expanded to "ASCII,JIS,UTF-8,EUC-JP,SJIS" */ $str = mb_convert_encoding($str, "EUC-JP", "auto");
Changelog
| Version | Change |
|---|---|
| 8.2.0 | mb_convert_encoding() will no longer return the following non text encodings: "Base64", "QPrint", "UUencode", "HTML entities", "7 bit" and "8 bit". |
| 8.0.0 | mb_convert_encoding() will now throw a ValueError when to_encoding is passed an invalid encoding. |
| 8.0.0 | mb_convert_encoding() will now throw a ValueError when from_encoding is passed an invalid encoding. |
| 8.0.0 | from_encoding is nullable now. |
| 7.2.0 | This function now also accepts an array as string. Formerly, only strings have been supported. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page mb_convert_encoding() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.