Tag: deprecated

PHP: Function utf8_decode() and utf8_encode() have been deprecated

php logo on purple background

The utf8_encode() and utf8_decode() functions in PHP are used for encoding and decoding strings between ISO-8859-1 (Latin-1) encoding and UTF-8 encoding.

While PHP’s standard library does include utf8_encode and utf8_decode functions, they are limited to converting between ISO-8859-1 (Latin-1) and UTF-8 encodings. It is important to note that these functions cannot be relied upon to detect and convert other character encodings, such as Windows-1252, UTF-16, and UTF-32, to UTF-8. Attempting to use these functions with arbitrary text can introduce bugs that may not produce any warnings or errors, but can result in unexpected and undesired outcomes.

Examples of common bugs that can occur include:

  • The Euro sign (character sequence \xE2\x82\xAC), when passed to utf8_encode function as utf8_encode("€") results in a a garbled (also called as “Mojibake”) text output of â¬.
  • The German Eszett character (ßcharacter sequence \xDF), when passed through utf8_encode("ß") results in Ã.

The utf8_encode and utf8_decode functions have been deprecated in PHP 8.2 due to their misleading function names, lack of error messages and warnings, and their inability to support character encodings other than ISO-8859-1.

As a result, using these functions in PHP 8.2 will emit a deprecation notice. It is recommended to use alternative functions or libraries that provide better support for handling different character encodings. These functions will be removed entirely in PHP 9.0, so it is important to migrate to alternative solutions as soon as possible to avoid compatibility issues in future versions of PHP.

utf8_encode('foo');

// Function utf8_encode() is deprecated in ... on line ...
uft8_decode('foo');

// Function uft8_decode() is deprecated in ... on line ...

Replacement for the deprecated functions

Instead, the PHP documentation recommends using the multibyte string functions that are part of the mbstring extension for handling multibyte encodings, including UTF-8. For example, the mb_convert_encoding() function can be used to convert strings between different character encodings, including to and from UTF-8.

Replacement for utf8_encode()

Here is an example of how to use mb_convert_encoding() to encode a string to UTF-8:

$string = "Some string with non-ASCII characters: é, ö, ü";
$utf8_string = mb_convert_encoding($string, 'UTF-8');

Replacement for utf8_decode()

And here is an example of how to use mb_convert_encoding() to decode an UTF-8 string:

$utf8_string = "Some UTF-8 encoded string: é, ö, ü";
$string = mb_convert_encoding($utf8_string, 'ISO-8859-1', 'UTF-8');

Source: https://php.watch/versions/8.2/utf8_encode-utf8_decode-deprecated