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 Approach | When to Use It |
|---|---|
mb_convert_encoding | Transcode text from one character encoding to another. |
| Related PHP approach | mb_check_encoding() validates; mb_detect_encoding() guesses; mb_convert_encoding() transforms. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Specify a known source encoding whenever possible; detection heuristics may guess incorrectly. |
| Confusing this API with a related operation | mb_check_encoding() validates; mb_detect_encoding() guesses; mb_convert_encoding() transforms. |
| Assuming output is identical across environments | Check 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.