Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix deprecated error utf8_encode() [duplicate]

90% of my website pages use the UTF-8 encoding feature to compile a DataTable.

$a[] = array_map('utf8_encode', $item);

With the old version 8.0 of PHP, everything was fine; however, in the new version, it tells me that this function is deprecated.

What is a valid alternative?

like image 930
Dmytro V Avatar asked Feb 01 '26 19:02

Dmytro V


2 Answers

We know that utf8_encode is deprecated since PHP 8.2.0 and will be removed in PHP 9 https://php.watch/versions/8.2/utf8_encode-utf8_decode-deprecated

So the alternative can be:

$oldSample = ["\x5A\x6F\xEB"];
$result= array_map
(
    function ($item){
        return mb_convert_encoding($item, "UTF-8", mb_detect_encoding($item));
    }, 
    $oldSample
);
var_dump($result);

Documentation:

  • https://www.php.net/manual/en/function.mb-convert-encoding.php
  • https://www.php.net/manual/en/function.mb-detect-encoding.php
like image 94
Eugene Kaurov Avatar answered Feb 03 '26 08:02

Eugene Kaurov


Use mb_convert_encoding to convert from ISO-8859-1 to UTF-8

Example:

$iso = "\x5A\x6F\xEB";
echo mb_convert_encoding($iso, 'UTF-8', 'ISO-8859-1'); // Zoë
like image 31
simon.ro Avatar answered Feb 03 '26 07:02

simon.ro