Page 1 of 1

UTF-8 encoding with json_encode

Posted: Mon Jun 01, 2009 11:16 am
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).

Re: UTF-8 encoding with json_encode

Posted: Mon Jun 01, 2009 12:23 pm
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.

Re: UTF-8 encoding with json_encode

Posted: Tue Jun 02, 2009 3:14 am
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!