Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

utf8_encode() is deprecated in PHP 8.2: what to use instead

ErrorDeprecated: Function utf8_encode() is deprecated

Short answer

utf8_encode() and utf8_decode() are deprecated since PHP 8.2.0. Replace them with mb_convert_encoding(), iconv() or UConverter::transcode(); all three give the same output for ISO-8859-1 input. If your text is really Windows-1252, name that as the source encoding, because the old functions garble characters like the euro sign.

  • Applies to PHP 8.2, PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List every call, then check what encoding the source text really has before you pick the replacement.

    grep -rn -E "utf8_(en|de)code" --include=*.php .
    php -m | grep -i -E "mbstring|iconv|intl"
  2. 2Match what you see, then jump to its fix

    1. 1Deprecated: Function utf8_encode() is deprecated, on a PHP 8.2 or newer serverPlain calls on ISO-8859-1 textFix
    2. 2After conversion, the euro sign or curly quotes are wrong or missingThe data is Windows-1252, not ISO-8859-1Fix
    3. 3Call to undefined function mb_convert_encoding()mbstring is not installed on the serverFix

What does the error look like?

  • Deprecated: Function utf8_encode() is deprecated
  • Deprecated: Function utf8_decode() is deprecated

Cause 1 of 3

Plain calls on ISO-8859-1 text

1Check

The old function converts from ISO-8859-1 to UTF-8. Confirm mbstring is loaded, because that is the replacement the manual lists first.

php -r "var_dump(function_exists('mb_convert_encoding'));"

2Fix

Name both encodings yourself. For decoding, swap the two encoding arguments.

// utf8_encode($s)
$utf8 = mb_convert_encoding($s, 'UTF-8', 'ISO-8859-1');

// utf8_decode($s)
$latin1 = mb_convert_encoding($s, 'ISO-8859-1', 'UTF-8');

3Verify

The manual's own example: the Latin-1 bytes for Zoe with a diaeresis come out as the hex string 5a6fc3ab.

php -r 'echo bin2hex(mb_convert_encoding("\x5A\x6F\xEB", "UTF-8", "ISO-8859-1"));'

Confirmed by php.net

Did this fix it?

Cause 2 of 3

The data is Windows-1252, not ISO-8859-1

1Check

The manual warns that many pages labelled ISO-8859-1 are really Windows-1252, and that characters such as the euro sign (byte 0x80) and curly quotes are not converted correctly by the old function. Check what the source system declares.

file -i legacy-export.txt

2Fix

Use Windows-1252 as the source encoding in the replacement call.

$utf8 = mb_convert_encoding($s, 'UTF-8', 'Windows-1252');

3Verify

The manual's example: byte 0x80 in Windows-1252 becomes the hex string e282ac, the UTF-8 euro sign.

php -r 'echo bin2hex(mb_convert_encoding("\x80", "UTF-8", "Windows-1252"));'

Confirmed by php.net

Did this fix it?

Cause 3 of 3

mbstring is not installed on the server

1Check

The check in the first step shows no mbstring line. The manual lists two other replacements.

php -m | grep -i -E "mbstring|iconv|intl"

2Fix

Use iconv(), or UConverter::transcode() when the intl extension is loaded. Both give the same result for ISO-8859-1 input as the mbstring call.

$utf8 = iconv('ISO-8859-1', 'UTF-8', $s);
// or, with intl:
$utf8 = UConverter::transcode($s, 'UTF8', 'ISO-8859-1');

3Verify

Run one known string through the call and compare the bytes.

php -r 'echo bin2hex(iconv("ISO-8859-1", "UTF-8", "\x5A\x6F\xEB"));'

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Store text as UTF-8 end to end, so no conversion is needed.
  • Wrap the conversion in one helper function so there is a single place to change.
  • Run the test suite with E_ALL and deprecations visible before raising the PHP version.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.