PHP utf8_en/decode 已弃用,我可以使用什么?

Dmy*_*o V 22 php encoding decoding deprecated

我的网站页面 90% 使用 utf8 编码功能来编译 DataTable。

$a[] = array_map('utf8_encode', $item);
Run Code Online (Sandbox Code Playgroud)

在旧版本的 php 8.0 中,一切都很好,在新版本中,当 $item 的值($item 是一个数组)为 null 时,它会给我一个错误。

什么是有效的替代方案?

Eug*_*rov 23

我们知道utf8_encode自 PHP 8.2.0 起已弃用,并将在 PHP 9 中删除https://php.watch/versions/8.2/utf8_encode-utf8_decode-deprecated

所以替代方案可以是:

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

文档:

  • 可以缩写为:``array_map(fn($item) => mb_convert_encoding($item, "UTF-8", mb_detect_encoding($item)), $oldSample);``` (2认同)

sim*_*.ro 10

使用mb_convert_encoding将 ISO-8859-1 转换为 UTF-8

\n

例子:

\n
$iso = "\\x5A\\x6F\\xEB";\necho mb_convert_encoding($iso, \'UTF-8\', \'ISO-8859-1\'); // Zo\xc3\xab\n
Run Code Online (Sandbox Code Playgroud)\n