Skip to content

utf8_decode()

The utf8_decode() function is a PHP built-in function that converts a string with UTF-8 encoding to ISO-8859-1 encoding. UTF-8 is a popular character encoding format that supports all Unicode characters, while ISO-8859-1 is a standard character encoding format that supports only a limited set of characters.

Note: utf8_decode() is deprecated as of PHP 8.2. For modern applications, use mb_convert_encoding($string, 'ISO-8859-1', 'UTF-8') instead.

The function is useful when you have UTF-8 encoded text that needs to be displayed or used in a system that only supports ISO-8859-1 encoding. By using utf8_decode(), you can convert the text to ISO-8859-1 encoding and ensure that it is displayed or used correctly.

Syntax

The syntax of the utf8_decode() function is as follows:

decode utf8 string in PHP

php
utf8_decode($string)

Where $string is the UTF-8 encoded string that you want to convert to ISO-8859-1 encoding.

Usage Examples

Let's take a look at some practical examples of using utf8_decode() in PHP.

Example 1: Converting UTF-8 Encoded Text to ISO-8859-1

Suppose you have a string with UTF-8 encoding that you want to convert to ISO-8859-1 encoding. You can use the utf8_decode() function to do this, like this:

Converting UTF-8 Encoded Text to ISO-8859-1 in PHP

php
<?php

$text = "Zoë";
$iso_text = utf8_decode($text);
echo bin2hex($iso_text);

?>

This code defines a string variable $text containing UTF-8 encoded text. It then uses the utf8_decode() function to convert the text to ISO-8859-1 encoding, and stores the result in $iso_text. Finally, it prints the hex representation of the ISO-8859-1 encoded text.

Return value & invalid sequences: The function returns the converted string, or false on failure. If the input contains invalid UTF-8 sequences, they are replaced with a question mark (?).

Example 2: Converting UTF-8 Encoded Text from XML

Suppose you have an XML file with UTF-8 encoded text that you want to read and convert to ISO-8859-1 encoding. You can use the SimpleXML library in PHP to read the XML file and the utf8_decode() function to convert the text, like this:

Converting UTF-8 Encoded Text from XML in PHP

php
$xml = simplexml_load_file("data.xml");
foreach ($xml->item as $item) {
  $title = utf8_decode($item->title);
  $description = utf8_decode($item->description);
  echo "$title: $description\n";
}

This code loads an XML file data.xml using the simplexml_load_file() function, and iterates over each <code><item></code> element using a foreach loop. Within the loop, it uses the utf8_decode() function to convert the UTF-8 encoded text in the <code><title></code> and <code><description></code> elements to ISO-8859-1 encoding, and stores the results in two new variables $title and $description. Finally, it prints the ISO-8859-1 encoded text to the console.

For this example to work, data.xml should contain a structure like this:

xml
<root>
  <item>
    <title>Example Title</title>
    <description>Example Description</description>
  </item>
</root>

Conclusion

In this article, we've discussed PHP's utf8_decode() function and how it can be used to convert UTF-8 encoded text to ISO-8859-1 encoded text. We've explained what the function does, its syntax, and provided examples of how it can be used in practical scenarios. By following these examples, you can easily use utf8_decode() to convert UTF-8 encoded text to ISO-8859-1 encoding and ensure that your text is displayed or used correctly in your system.

Practice

What does the utf8_decode function in PHP do?

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.