utf8_encode() is deprecated in PHP 8.2: what to use instead
Deprecated: Function utf8_encode() is deprecatedShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
Deprecated: Function utf8_encode() is deprecated, on a PHP 8.2 or newer serverPlain calls on ISO-8859-1 textFix - 2
After conversion, the euro sign or curly quotes are wrong or missingThe data is Windows-1252, not ISO-8859-1Fix - 3
Call to undefined function mb_convert_encoding()mbstring is not installed on the serverFix
- 1
What does the error look like?
Deprecated: Function utf8_encode() is deprecatedDeprecated: 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
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.txt2Fix
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
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
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix