2

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.

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yup that was it! Thank you so much.
0
return array( 'name' => utf8_decode($prestore[0]),
'online_players' => utf8_decode($prestore[1]),
'maximum_players' => utf8_decode($prestore[2]));

Try this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.