【问题标题】:Convert PHP json encoded UTF8 string into regular characters将 PHP json 编码的 UTF8 字符串转换为常规字符
【发布时间】:2012-04-26 20:34:02
【问题描述】:

我在 PHP (5.3) 中有一个 UTF8_encoded 数组,但似乎无法以正常方式操作结果。

在 PHP 中回显数据时,结果会正确显示,但在测试变量时(例如使用 is_numeric)会失败。

如何转换此数组以使数据在 PHP 中正常使用?

getInfo($host,$port){
....
$prestore = mb_split("\xc2\xA7", utf8_encode($response));

return array( 'name' => $prestore[0],
'online_players' => $prestore[1],
'maximum_players' => $prestore[2]);
}

$data = getInfo($host,$port);
var_dump($data);

$data2 = json_encode(getInfo($host,$port));
echo $data2;

PHP Var dump 的数据结果是正确的,但字符串长度全部关闭。

Array(3) {
  ["name"]=>
  string(23) "MC-Outbreak"
  ["online_players"]=>
  string(5) "13"
  ["maximum_players"]=>
  string(4) "70"
}

PHP json_encode 结果如下所示。

{"name":"\u0000M\u0000C\u0000-\u0000O\u0000u\u0000t\u0000b\u0000r\u0000e\u0000a\u0000k\u0000","online_players":"\u00001\u00003\u0000","maximum_players":"\u00007\u00000"}

我花了几个小时尝试不同的东西,但现在完全卡住了!任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: php json encoding utf-8


    【解决方案1】:

    您的文本采用 UTF-16BE 格式。使用 iconv 先将其转换为 UTF-8。

    【讨论】:

    • 是的,就是这样!非常感谢。
    【解决方案2】:
    return array( 'name' => utf8_decode($prestore[0]),
    'online_players' => utf8_decode($prestore[1]),
    'maximum_players' => utf8_decode($prestore[2]));
    

    试试这个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多