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.

1
  • Did you try leaving out the decoding? Commented Aug 25, 2014 at 10:09

1 Answer 1

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.

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

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.