PHP mb_convert_encoding() Function

Definition
In PHP, mb_convert_encoding() converts a string, or an array of strings, from one character encoding to another.
It is part of the mbstring extension, so it works the same way on every server that has mbstring.
Syntax
Call mb_convert_encoding() with the values you need. Start with this line.
$out = mb_convert_encoding($text, "UTF-8", "ISO-8859-1");
| Call | What it does |
|---|
mb_convert_encoding($s, "UTF-8", "ISO-8859-1") | Converts Latin-1 text to UTF-8. |
mb_convert_encoding($s, "ISO-8859-1", "UTF-8") | Converts UTF-8 text to Latin-1. |
The order is: text, target encoding, source encoding. The target comes first.
Library declarations
mb_convert_encoding(array|string $string, string $to_encoding, array|string|null $from_encoding = null): array|string|false
Parameters
| Parameter | Description |
|---|
| string | Required. The text, or an array of texts, to convert. |
| to_encoding | Required. The encoding you want. |
| from_encoding | Optional. The encoding you have. Default is the internal encoding. |
Return Value
| Return Value | Description |
|---|
| array|string|false | The converted string or array, or false on failure. |
PHP Examples
Example 1 — Convert Latin-1 to UTF-8
Change the encoding of a short string and compare byte lengths.
<?php
$latin1 = "caf\xE9";
$utf8 = mb_convert_encoding($latin1, "UTF-8", "ISO-8859-1");
echo $utf8, PHP_EOL;
echo strlen($latin1), " -> ", strlen($utf8), PHP_EOL;
How It Works
- Create a Latin-1 string. The byte
\xE9 is é. - Call mb_convert_encoding(). The target is the second argument.
- The source is the third argument.
- The letter
é takes 2 bytes in UTF-8, so the length grows.
Example 2 — See What Happens to Missing Letters
The euro sign does not exist in ISO-8859-1. It is replaced by a question mark.
<?php
$text = "café €";
$latin1 = mb_convert_encoding($text, "ISO-8859-1", "UTF-8");
echo bin2hex($latin1), PHP_EOL;
How It Works
- Convert UTF-8 to ISO-8859-1.
- The letter
é becomes the single byte e9. - The euro sign cannot be stored, so it becomes
?, which is 3f. - Use
bin2hex() to see the bytes.
Frequently Asked Questions
mb_convert_encoding() converts a string or an array from one character encoding to another.
mb_convert_encoding(array|string $string, string $to_encoding, array|string|null $from_encoding = null): array|string|false.
They are replaced with a question mark by default.
Yes. Pass an array or a comma-separated list, and PHP will guess which one fits. A single known source is more reliable.
Both work for common cases. mb_convert_encoding() is part of mbstring. iconv() depends on the system library and supports //TRANSLIT.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful