0

I have list of player name. But the problem is the player name contains some non utf-8 characters. so i am having following error.

json_encode(): Invalid UTF-8 sequence in argument

i have used following code to overcome it, but it didn't work.

utf8_encode($player->name);

and

$player_name = iconv(mb_detect_encoding($player->name, mb_detect_order(), true), "UTF-8", $player->name);

thanks in advance.

user1795109
  • 678
  • 2
  • 6
  • 14
  • Can you give an example player name where the error occur? – sofl May 22 '14 at 12:49
  • this is the json working on the local pc. & it seems to have no problem. But on live server we encounter with the problem.{ "player_id" : 125, "player_name" : "J\u00c3\u0083\u00c2\u00balio C\u00c3...", "player_country" : "35", "player_country_name" : "Brazil", "player_team" : "Brasil", "player_role" : "Goalkeeper", "player_price" : 0, "substitute" : 0, "captain" : 1, "vice_captain" : 0 } – user1795109 May 22 '14 at 12:51
  • when it is rendered on the browser it displays Júlio Cà – user1795109 May 22 '14 at 12:56
  • 1
    Yes i saw. Seems indeed broken. Maybe this library could help you https://github.com/neitanod/forceutf8 see also here http://stackoverflow.com/questions/1344692/i-need-help-fixing-broken-utf8-encoding – sofl May 22 '14 at 12:57
  • possible duplicate of [json\_encode(): Invalid UTF-8 sequence in argument](http://stackoverflow.com/questions/10205722/json-encode-invalid-utf-8-sequence-in-argument) – Matt S Jul 22 '15 at 19:12

1 Answers1

2

Just had the same issue and was able to solve it by forcing UFT-8 with mb_convert_encoding():

$string = mb_convert_encoding($string,'UTF-8','UTF-8');
John Veldboom
  • 2,049
  • 26
  • 29