~ overflow ~

Tag: utf-8

json decode fails on non utf-8

by z3n on Mar.16, 2010, under Coding, Tips & Hints

Problem:

When sending a non utf-8 string as json, the decoding fails.

Solution:

PHP works as utf-8 as default, since i’m using strings with accents (áéíóú..) those are taken as iso-8859-1. Client-side script will not send as utf-8, not even if you force it, so the best solution is convert the json object’s encoding. You may also want to encode your string as plain chars (I use base64) to avoid issues with IE.

Code would look like this:

$json=json_decode(iconv(‘ISO-8859-1′,’UTF-8′,base64_decode($input)),true);

If you’re working with different charsets just change the iso-8859-1, remember that if you’re working with multibyte chars, such as japanese, chinese, etc, you will need to use the mb functions instead.

Sources:

Pablo Viquez (A solution pretty much like mine but for sending data instead)

2 Comments :, , , , more...

Fix Japanese File Name on Linux

by z3n on Mar.06, 2009, under Linux Happyness

Problem:

Japanese filenames are screwed up on linux, when you try to copy them to windows their encoding is different. Or when you write them on linux and want to use on a windows machine or even by http or ftp they are all wrong.

Solution:

Apart from all the encoding mess, apparently linux encodes filenames as UTF-16 which is different from windows’ UTF-8 default, i say apparently because all the programs i used to test the charset on the linux stated that it was UTF-8 actually, which contradicts all the sites about linux’s default charset being UTF-16 … but this is just lame, basically what you need to do is convert the file names to Shift_JIS which is the most compatible format for windows, i wrote a php script to fix a folder or even the whole tree if you want. the source:

http://www.overflow.biz/blog/~scripts/convert2shift_jis.phps

by default this script will only rename the files it’s into, but you can easly tune it up to rename everything.

Leave a Comment :, , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!