Gadgets, Computers and Nerdy Stuff
PHP: Replace \u characters in json string
In PHP, if you JSON-encode a string that contains accented characters, the result contains unicode sequences (for example: \u00e9 represents the é character).
<?php $string = 'åbcdéfg'; print json_encode($string) . "\n"; ?>
Running this gives the following output:
"\u005e5bcd\u00e9fg"
If you want to replace the unicode sequences back to their character representation, here’s what you need:
function jsonRemoveUnicodeSequences($struct) {
return preg_replace("/\\\\u([a-f0-9]{4})/e", "iconv('UCS-4LE','UTF-8',pack('V', hexdec('U$1')))", json_encode($struct));
}
Example:
<?php $string = 'åbcdéfg'; print jsonRemoveUnicodeSequences($string) . "\n"; ?>
Results in:
"åbcdéfg"
| Print article | This entry was posted by Arjan on January 10, 2011 at 21:12, and is filed under PHP. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 2 years ago
I was trying for like 1 hour to make this preg_replace work, PHP is really picky with preg_replace modifiers :@ Thanks a lot!
about 2 years ago
I tried so many ‘solutions’ for this problem and this was the only one that worked, thank you!
about 2 years ago
Thanks for this function, finally solved my problem!
about 1 year ago
Thanks a lot. I wonder why PHP doesn’t handle it !?