PHP mb_convert_encoding() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
mbstring

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.

PHP
$out = mb_convert_encoding($text, "UTF-8", "ISO-8859-1");
CallWhat 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
PHP
mb_convert_encoding(array|string $string, string $to_encoding, array|string|null $from_encoding = null): array|string|false

Parameters

ParameterDescription
stringRequired. The text, or an array of texts, to convert.
to_encodingRequired. The encoding you want.
from_encodingOptional. The encoding you have. Default is the internal encoding.

Return Value

Return ValueDescription
array|string|falseThe 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
<?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

  1. Create a Latin-1 string. The byte \xE9 is é.
  2. Call mb_convert_encoding(). The target is the second argument.
  3. The source is the third argument.
  4. 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
<?php
$text = "café €";

$latin1 = mb_convert_encoding($text, "ISO-8859-1", "UTF-8");

echo bin2hex($latin1), PHP_EOL;

How It Works

  1. Convert UTF-8 to ISO-8859-1.
  2. The letter é becomes the single byte e9.
  3. The euro sign cannot be stored, so it becomes ?, which is 3f.
  4. Use bin2hex() to see the bytes.

Example 3 — Convert a Whole Array

Pass an array to convert every value at once.

PHP
<?php
$row = ["city" => "Z\xFCrich", "name" => "Andr\xE9"];

$utf8 = mb_convert_encoding($row, "UTF-8", "ISO-8859-1");

print_r($utf8);

How It Works

  1. Create an array of Latin-1 values.
  2. Pass the array to mb_convert_encoding().
  3. Each value is converted. The keys are converted too.
  4. Use it for database rows or CSV lines in the old encoding.

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.

Next: mb_detect_encoding()

mb_detect_encoding() guesses the character encoding of a string.

mb_detect_encoding() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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