RyanManard
RyanManard

Reputation: 35

Convert PHP json encoded UTF8 string into regular characters

I have a UTF8_encoded array in PHP (5.3) but can't seem to use manipulate the results in a normal way.

When echo'ing the data in PHP the results show up correctly but when testing the variable (eg using is_numeric) it fails.

How can I convert this array to make the data useable as normal in 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 of data results are correct but the string lengths are all off.

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

PHP json_encode results look like this.

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

I spent a couple hours trying different things but am completely stuck now! Any help would be appreciated.

Thanks.

Upvotes: 2

Views: 1448

Answers (2)

Subhojit Mukherjee
Subhojit Mukherjee

Reputation: 719

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

Try this

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798626

Your text is in UTF-16BE. Use iconv to convert it to UTF-8 first.

Upvotes: 2

Related Questions