In this tutorial, we will learn how we can use the utf8_encode() function to convert a string from iso-8859-1 to utf-8.
ISO-8859-1 is a character encoding standard that was published by the International Organization for Standardization (ISO) in 1987. It is also known as Latin-1 or Western European (ISO).
This character encoding is used to represent the characters of the Latin alphabet used in Western European languages, such as English, French, German, Spanish, and many others. It includes 256 characters, including the 26 letters of the basic Latin alphabet, digits, punctuation marks, and other symbols.
ISO-8859-1 is widely used in various computer systems, including the Internet. However, it has limitations, as it does not support characters used in other languages, such as Chinese, Japanese, or Arabic. As a result, it has been largely superseded by Unicode, which supports a much broader range of characters and is the current standard for character encoding in computing.
You may therefore come across instances where you need to convert a given string from
ISO-8859-1 encoding to
PHP utf8_encode() Function
This function converts the string
string from the
ISO-8859-1 encoding to
UTF-8. The function syntax is as shown below:
utf8_encode(string $string): string
The function accepts one main parameter:
string- denotes the string you wish to convert to UTF8 encoding. The input must be a valid
The function will then return the UTF-8 translation of the input string.
The following example demonstrates the usage of the
<?php // Convert the string 'Zoë' from ISO 8859-1 to UTF-8 $iso8859_1_string = "\x5A\x6F\xEB"; $utf8_string = utf8_encode($iso8859_1_string); echo bin2hex($utf8_string), "\n"; ?>
PHP 8 Compatible Conversion
As of writing this tutorial, the
utf8_encode() function in PHP is deprecated in PHP 8 and above. It is therefore highly discouraged to use this function in your development.
Let us look at a more compatible alternative.
PHP mb_convert_encoding() Function
As the name suggests, the function allows you to convert a given string from encoding to another. The function syntax is as shown:
mb_convert_encoding(array|string $string, string $to_encoding, array|string|null $from_encoding = null): array|string|false
The function accepts three main parameters. These include:
string- this specifies the string or array whose encoding you wish to convert.
to_encoding- specifies the target encoding.
from_encoding- this specifies the current encoding for the input string. If not provided, PHP will attempt to guess the current encoding.
The function will then return an encoded string or array.
Example - Convert From ISO 8859-1 To UTF8
The following example demonstrates how we can use the
mb_convert_encoding() function to convert from ISO 8859-1 to UTF8.
// Original string encoded in ISO-8859-1 $original_string = "This is a string encoded in ISO-8859-1."; // Convert the string to UTF-8 $utf8_string = mb_convert_encoding($original_string, 'UTF-8', 'ISO-8859-1'); // Output the converted string echo $utf8_string;
In this post, we discussed how you can use the
utf8_encode() function to convert a string from
utf8 encoding. You also discovered a similar method that is compatible with PHP 8 and above for security and maximum compatibility.
Enjoying our PHP tutorials? Check the others provided below.