0

I am have retrieved a string from data base that contains Unicode characters, After that I have utf8_decoded them so I can read them clearly,

Then I passed the string to json_decode but it return null! without utf8_decode the json_decode return an array with é characters.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
user3336078
  • 53
  • 1
  • 11

1 Answers1

2

utf8_decode converts a string's encoding from UTF-8 to ISO-8859-1, a.k.a. Latin-1.
json_decode expects, requires and returns UTF-8 encoded strings.
That's why it's obviously not working.

The string you get from the database is apparently UTF-8 encoded, which is good. You must not convert it to Latin-1 before you decode the JSON. You should also not convert it afterwards, just keep everything in UTF-8. The only problem you have is that you're not correctly instructing your browser to deal with UTF-8. The quick answer is to set a proper HTTP header:

header('Content-Type: text/html; charset=UTF-8');

For the longer and more nuanced answer(s), see UTF-8 all the way through, Handling Unicode Front To Back In A Web App and What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889