Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP utf8_en/decode deprecated, what can i use?

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 279
Dmytro V Avatar asked Sep 03 '25 02:09

Dmytro V


1 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 183
Eugene Kaurov Avatar answered Sep 05 '25 01:09

Eugene Kaurov