utf8_encode()
The utf8_encode() function is a PHP built-in function that converts a string with ISO-8859-1 encoding to UTF-8 encoding. ISO-8859-1 is a standard character
The utf8_encode() function is a PHP built-in function that converts a string from ISO-8859-1 (Latin-1) encoding to UTF-8. It is useful when you receive Latin-1 text — from a legacy database, a file, or an old API — and need it to display correctly in a system that expects UTF-8.
This page explains what the function does, how it works at the byte level, when (and when not) to use it, and the modern replacements you should prefer in current PHP versions.
Deprecated, then removed.
utf8_encode()was deprecated in PHP 8.2 and removed in PHP 8.3. New code should usemb_convert_encoding()oriconv()instead — see Modern replacements below. This page documents the legacy function for the many codebases that still rely on it.
What "encoding" means here
A character encoding is a mapping between characters and the bytes that represent them. ISO-8859-1 is a single-byte encoding: every character is exactly one byte (256 possible values), which covers Western European letters such as é, ñ, and ü. UTF-8 is a variable-width encoding where those same accented characters take two bytes.
utf8_encode() does one specific job: it reads each byte of the input as an ISO-8859-1 code point and re-writes it as the equivalent UTF-8 byte sequence. It does not detect the input encoding — it always assumes the input is ISO-8859-1. If you feed it a string that is already UTF-8, you get garbled "mojibake" (double-encoded) output.
Syntax
utf8_encode(string $string): string| Parameter | Description |
|---|---|
$string | The ISO-8859-1 (Latin-1) encoded string to convert. |
Return value: the same text re-encoded as UTF-8.
Usage Examples
Let's take a look at some practical examples of using utf8_encode() in PHP.
Example 1: Converting ISO-8859-1 text to UTF-8
Suppose you have a string with ISO-8859-1 encoding that you want to convert to UTF-8. You can use utf8_encode() to do this:
This code defines a string variable $text holding ISO-8859-1 text, converts it to UTF-8 with utf8_encode(), and prints the result. Note the caveat in the comment: the source string itself must actually be ISO-8859-1. If your editor saves the file as UTF-8, the é is already two bytes and utf8_encode() will mangle it into é.
Example 2: Seeing the byte-level change
To make the conversion concrete, inspect the byte length before and after. The accented character grows from one byte to two:
<?php
$latin1 = "\xE9"; // a single byte: 'é' in ISO-8859-1
echo strlen($latin1); // 1
$utf8 = utf8_encode($latin1);
echo strlen($utf8); // 2 -> the bytes 0xC3 0xA9
echo bin2hex($utf8); // c3a9
?>strlen() counts bytes, not characters, so the same letter reports a length of 1 in Latin-1 and 2 in UTF-8. This single-to-double byte expansion is exactly what makes the converted text render correctly in a UTF-8 context.
Example 3: Converting ISO-8859-1 Encoded Text from XML
Suppose you have an XML file declared as ISO-8859-1 that you want to read and convert to UTF-8. You can use the SimpleXML library to read the file and utf8_encode() to convert each value:
<?php
$xml = simplexml_load_file("data.xml");
foreach ($xml->item as $item) {
$title = utf8_encode($item->title);
$description = utf8_encode($item->description);
echo "$title: $description\n";
}
?>This loads an XML file declared as ISO-8859-1 with simplexml_load_file(), iterates over each <item> element, and converts the <title> and <description> text to UTF-8 before printing. (The SimpleXMLElement values are cast to strings by utf8_encode().)
When to use it (and when not to)
Reach for utf8_encode() only when all of these are true:
- The input is genuinely ISO-8859-1 / Latin-1 (not Windows-1252, not already UTF-8).
- You are on PHP 8.2 or earlier, where the function still exists.
- You want a quick, dependency-free Latin-1 → UTF-8 conversion.
Avoid it when:
- The source might be Windows-1252 (common for text from Windows / Excel). Windows-1252 reuses the
0x80–0x9Frange for characters like€and curly quotes that ISO-8859-1 leaves undefined — those will be lost or wrong. Usemb_convert_encoding($s, 'UTF-8', 'Windows-1252')instead. - You don't actually know the input encoding. Detect or declare it explicitly rather than guessing.
- You target PHP 8.3+, where the function is gone entirely.
Modern replacements
Since utf8_encode() is removed in PHP 8.3, prefer the multibyte-string or iconv functions, which let you name the source encoding explicitly:
<?php
$latin1 = "\xE9"; // 'é' in ISO-8859-1
// mbstring extension (recommended)
$utf8 = mb_convert_encoding($latin1, 'UTF-8', 'ISO-8859-1');
// iconv extension
$utf8 = iconv('ISO-8859-1', 'UTF-8', $latin1);
echo bin2hex($utf8); // c3a9 in both cases
?>Both produce the same two bytes (0xC3 0xA9) as utf8_encode(), but they make the source encoding part of the call — so they also work for Windows-1252, ISO-8859-15, and dozens of other encodings.
Related functions
utf8_decode()— the inverse: convert UTF-8 back to ISO-8859-1.json_encode()— produces UTF-8 output and escapes multibyte characters.- PHP Strings — overview of working with text in PHP.
Conclusion
utf8_encode() converts ISO-8859-1 (Latin-1) text to UTF-8 by re-encoding each byte — turning single-byte accented characters into their two-byte UTF-8 form. It is convenient but blind to the real input encoding, and it is deprecated in PHP 8.2 and removed in PHP 8.3. For any new code, use mb_convert_encoding() or iconv(), which let you specify the source encoding explicitly and handle a far wider range of character sets.