UTF-8 encoding with json_encode

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

UTF-8 encoding with json_encode

Post by mattpointblank »

Hey all.

Using some JSON to pass data using AJAX - running into issues where the encoding just stops abruptly when it encounters this text:

"...in São Paulo..."

This bug suggests it's not just me: http://bugs.php.net/bug.php?id=43941

I can't upgrade my php install on this server so does anyone have any alternatives? This content is coming from a database and may contain foreign characters frequently. I read somewhere else about using utf8_encode but this breaks the HTML I'm passing through the JSON (eg, a </h1> tag becomes <\/h1> and messes up my styling) - any tips? If it helps, the webpage itself is encoded in ISO-8859-1 (although changing it to UTF-8 through Firefox doesn't seem to help).
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: UTF-8 encoding with json_encode

Post by requinix »

Try changing the encoding to something else first (like whatever PHP uses internally, with utf8_decode), then JSON-encode, but then run that back through utf8_encode afterward.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: UTF-8 encoding with json_encode

Post by mattpointblank »

I think I fixed it by changing the HTML encoding to utf-8 in the code itself (not through Firefox) and then using utf_encode on the input - thanks though!
Post Reply