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 encoding to UTF-8. It is useful when you need to display or process ISO-8859-1 text in a system that only supports UTF-8, ensuring characters are rendered correctly.
Syntax
The syntax of the utf8_encode() function is as follows:
syntax of the utf8_encode() function
utf8_encode($string)Where $string is the ISO-8859-1 encoded string that you want to convert to UTF-8 encoding.
Usage Examples
Let's take a look at some practical examples of using utf8_encode() in PHP.
Example 1: Converting ISO-8859-1 Encoded Text to UTF-8
Suppose you have a string with ISO-8859-1 encoding that you want to convert to UTF-8 encoding. You can use the utf8_encode() function to do this, like this:
Converting ISO-8859-1 Encoded Text to UTF-8
<?php
// Note: This example assumes the PHP source file is saved in ISO-8859-1.
// If your editor saves files as UTF-8 by default, utf8_encode() will double-encode the string.
$text = "Café au lait";
$utf8_text = utf8_encode($text);
echo $utf8_text;
?>This code defines a string variable $text with ISO-8859-1 encoded text "Café au lait". It then uses the utf8_encode() function to convert the text to UTF-8 encoding, and stores the result in a new variable $utf8_text. Finally, it prints the UTF-8 encoded text to the console.
Example 2: Converting ISO-8859-1 Encoded Text from XML
Suppose you have an XML file with ISO-8859-1 encoded text that you want to read and convert to UTF-8 encoding. You can use the SimpleXML library in PHP to read the XML file and the utf8_encode() function to convert the text, like this:
Converting ISO-8859-1 Encoded Text from XML
<?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 code loads an XML file "data.xml" using the simplexml_load_file() function, and iterates over each <item> element using a foreach loop. Within the loop, it uses the utf8_encode() function to convert the ISO-8859-1 encoded text in the <title> and <description> elements to UTF-8 encoding, and stores the results in two new variables $title and $description. Finally, it prints the UTF-8 encoded text to the console.
Note: utf8_encode() is deprecated in PHP 8.2. For modern applications, consider using mb_convert_encoding($string, 'UTF-8', 'ISO-8859-1') or iconv('ISO-8859-1', 'UTF-8', $string) for better cross-platform compatibility and explicit encoding handling.
Conclusion
In this article, we've discussed PHP's utf8_encode() function and how it can be used to convert ISO-8859-1 encoded text to UTF-8. 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 ensure your text data is correctly encoded for modern web applications.
Practice
What is the purpose of the utf8_encode() function in PHP?