Content · Programming · PHP

PHP mb_convert_encoding() Function: Usage and Examples

Learn how to transcode text from one character encoding to another in PHP with syntax, clear code examples, practical use, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When working with PHP, you may need to transcode text from one character encoding to another. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.

mb_convert_encoding: Converting the wrong source encoding can corrupt otherwise valid text.

What Is mb_convert_encoding in PHP?

Transcode text from one character encoding to another. Converting the wrong source encoding can corrupt otherwise valid text.

Syntax:

mb_convert_encoding(array|string $string, string $toEncoding, array|string|null $fromEncoding = null): array|string|false

Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.

Important: Specify a known source encoding whenever possible; detection heuristics may guess incorrectly.

Example 1: Basic Usage

This example introduces the operation with a short and predictable input.

<?php

echo mb_convert_encoding("Café", "UTF-8", "UTF-8");

The code focuses on the main operation. Converting the wrong source encoding can corrupt otherwise valid text.

Example 2: Another Case

Now compare a different input, locale, date, or option to see another part of the behavior.

<?php

$latin1 = "Caf\xE9";
echo mb_convert_encoding($latin1, "UTF-8", "ISO-8859-1");

What changes? Specify a known source encoding whenever possible; detection heuristics may guess incorrectly.

Practical Example: Applying It in an Application

Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.

<?php

$legacyRows = ["M\xFCller", "Caf\xE9"];
foreach ($legacyRows as $row) {
    echo mb_convert_encoding($row, "UTF-8", "ISO-8859-1") . PHP_EOL;
}

The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Converting the wrong source encoding can corrupt otherwise valid text.

Important Behavior and Edge Cases

  • Core behavior: Converting the wrong source encoding can corrupt otherwise valid text.
  • Inputs and results: Specify a known source encoding whenever possible; detection heuristics may guess incorrectly.
  • Alternatives: mb_check_encoding() validates; mb_detect_encoding() guesses; mb_convert_encoding() transforms.
  • Compatibility: mbstring is required for mb_* functions.

Comparison With Related Functions

Function or ApproachWhen to Use It
mb_convert_encodingTranscode text from one character encoding to another.
Related PHP approachmb_check_encoding() validates; mb_detect_encoding() guesses; mb_convert_encoding() transforms.

Common Beginner Mistakes

MistakeCorrect Approach
Using an incorrect input or optionSpecify a known source encoding whenever possible; detection heuristics may guess incorrectly.
Confusing this API with a related operationmb_check_encoding() validates; mb_detect_encoding() guesses; mb_convert_encoding() transforms.
Assuming output is identical across environmentsCheck return values and required extensions or versions. mbstring is required for mb_* functions.

Key Takeaways

  • mb_convert_encoding helps you transcode text from one character encoding to another.
  • Converting the wrong source encoding can corrupt otherwise valid text.
  • Specify a known source encoding whenever possible; detection heuristics may guess incorrectly.

Use mb_convert_encoding when you need to transcode text from one character encoding to another. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.

Share this resourceSend this page to someone who may find it useful.