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



我的网站90%的页面使用utf8编码特性来编译DataTable

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

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

什么是有效的替代?

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

可以是:

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

文档:

  • https://www.php.net/manual/en/function.mb-convert-encoding.phphttps://www.php.net/manual/de/function.mb-detect-encoding.php

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

的例子:

$iso = "x5Ax6FxEB";
echo mb_convert_encoding($iso, 'UTF-8', 'ISO-8859-1'); // Zoë

我只是在array_map('utf8_encode', $item):

foreach(array_keys($item) as $key)
$item[$key] = utf8_encode($item[$key] ?? '');

效果很好

相关内容

最新更新