php mb_convert_variables 递归错误
php mb_convert_variables RECURSION error
我在将一些数组转换为 UTF-8 时遇到问题
基本上我是从网站中提取元标记,如果字符集不是 UTF-8,我会尝试将它们转换为 UTF-8,以便它们可以正确存储和显示。转换前的原始数组如下
print_r($details);
[base] => http://www.example.com/something/page/
[charset] => iso-8859-1
[favicon] => http://www.example.com/favicon.ico
[meta] => Array
(
[description] => Some Description
[keywords] =>
)
[images] => Array
(
[0] => http://cdn.example.com/wp-content/themes/original/images/logo.jpg
[1] => http://cdn.examplecom/wp-content/uploads/2016/10/EXAMPLE-imageoptim-twitter-bird-16x16.png
)
[openGraph] => Array
(
[locale] => en_GB
[type] => article
[title] => Some Title
[description] => Some Description
[url] => http://www.example.com/something/page/
[site_name] => EXAMPLE
[image] => http://cdn-r1.example.com/wp-content/uploads/2017/02/621-Example-fb.jpg
[image:width] => 736
[image:height] => 378
[imagePath] => http://cdn-r1.example.com/wp-content/uploads/2017/02/621-Example-fb.jpg
)
[title] => Some Title
[url] => http://www.example.com/something/page/
[url_description] => Some Description
//End of print_r();
所以上面的数组很好,很合适,但是因为我无法知道文本是否会正确显示,所以我会把它转换成 UTF-8,因为网站统计了它的字符集不是 utf-8。
我把上面的数组通过下面
mb_convert_variables('utf-8', $details['charset'], $details);
注意 $details['meta'] 和 $details['openGraph'] 的输出很奇怪。该数组已替换为 RECURSION。我尝试 google 这个,但我找不到任何东西。
print_r($details);
//Note: This is the exact print_r results with the *RECURSIVE* words.
[base] => http://www.example.com/something/page/
[charset] => iso-8859-1
[favicon] => http://www.example.com/favicon.ico
[meta] => Array
*RECURSION*
[images] => Array
(
[0] => http://cdn.example.com/wp-content/themes/original/images/logo.jpg
[1] => http://cdn.examplecom/wp-content/uploads/2016/10/EXAMPLE-imageoptim-twitter-bird-16x16.png
)
[openGraph] => Array
*RECURSION*
[title] => Some Title
[url] => http://www.example.com/something/page/
[url_description] => Some Description
由于上述原因,我无法将其输出为 json 和
echo json_encode($details);
die();
但是...如果我要对其进行序列化和反序列化,再一次没问题。
echo json_encode(unserialize(serialize($details)));
die();
我可以知道我的数组或代码有什么问题吗?我可以使用我当前的序列化和反序列化操作,但我宁愿在它影响我所有未来的数据之前知道这个问题。
它的错误,已报告,手动循环并使用 mb_convert_encoding
转换每个字符串
function mb_convert_array($to_encoding, $from_encoding, $array)
{
foreach($array as $key => $value)
{
if(is_array($value))
{
$array[$key] = mb_convert_array($to_encoding, $from_encoding, $value);
}
else
{
$array[$key] = mb_convert_encoding($value, $to_encoding, $from_encoding);
}
}
return $array;
}
mb_convert_array('utf-8', $details['charset'], $details);
我在将一些数组转换为 UTF-8 时遇到问题
基本上我是从网站中提取元标记,如果字符集不是 UTF-8,我会尝试将它们转换为 UTF-8,以便它们可以正确存储和显示。转换前的原始数组如下
print_r($details);
[base] => http://www.example.com/something/page/
[charset] => iso-8859-1
[favicon] => http://www.example.com/favicon.ico
[meta] => Array
(
[description] => Some Description
[keywords] =>
)
[images] => Array
(
[0] => http://cdn.example.com/wp-content/themes/original/images/logo.jpg
[1] => http://cdn.examplecom/wp-content/uploads/2016/10/EXAMPLE-imageoptim-twitter-bird-16x16.png
)
[openGraph] => Array
(
[locale] => en_GB
[type] => article
[title] => Some Title
[description] => Some Description
[url] => http://www.example.com/something/page/
[site_name] => EXAMPLE
[image] => http://cdn-r1.example.com/wp-content/uploads/2017/02/621-Example-fb.jpg
[image:width] => 736
[image:height] => 378
[imagePath] => http://cdn-r1.example.com/wp-content/uploads/2017/02/621-Example-fb.jpg
)
[title] => Some Title
[url] => http://www.example.com/something/page/
[url_description] => Some Description
//End of print_r();
所以上面的数组很好,很合适,但是因为我无法知道文本是否会正确显示,所以我会把它转换成 UTF-8,因为网站统计了它的字符集不是 utf-8。
我把上面的数组通过下面
mb_convert_variables('utf-8', $details['charset'], $details);
注意 $details['meta'] 和 $details['openGraph'] 的输出很奇怪。该数组已替换为 RECURSION。我尝试 google 这个,但我找不到任何东西。
print_r($details);
//Note: This is the exact print_r results with the *RECURSIVE* words.
[base] => http://www.example.com/something/page/
[charset] => iso-8859-1
[favicon] => http://www.example.com/favicon.ico
[meta] => Array
*RECURSION*
[images] => Array
(
[0] => http://cdn.example.com/wp-content/themes/original/images/logo.jpg
[1] => http://cdn.examplecom/wp-content/uploads/2016/10/EXAMPLE-imageoptim-twitter-bird-16x16.png
)
[openGraph] => Array
*RECURSION*
[title] => Some Title
[url] => http://www.example.com/something/page/
[url_description] => Some Description
由于上述原因,我无法将其输出为 json 和
echo json_encode($details);
die();
但是...如果我要对其进行序列化和反序列化,再一次没问题。
echo json_encode(unserialize(serialize($details)));
die();
我可以知道我的数组或代码有什么问题吗?我可以使用我当前的序列化和反序列化操作,但我宁愿在它影响我所有未来的数据之前知道这个问题。
它的错误,已报告,手动循环并使用 mb_convert_encoding
function mb_convert_array($to_encoding, $from_encoding, $array)
{
foreach($array as $key => $value)
{
if(is_array($value))
{
$array[$key] = mb_convert_array($to_encoding, $from_encoding, $value);
}
else
{
$array[$key] = mb_convert_encoding($value, $to_encoding, $from_encoding);
}
}
return $array;
}
mb_convert_array('utf-8', $details['charset'], $details);