1 common.inc | backdrop_json_decode_unicode($json) |
Decode Unicode characters in JSON strings.
This function is no longer used directly in core. Use backdrop_json_encode() to format JSON.
Deprecated
since 1.22.0
File
- core/
includes/ common.inc, line 5906 - Common functions that many Backdrop modules will need to reference.
Code
function backdrop_json_decode_unicode($json) {
watchdog_deprecated_function('common', __FUNCTION__);
// Convert escaped characters, such as \u00e9 to the unescaped version (é).
// See http://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-cha
$decode_callback = function($match) {
// Leave control characters such as NULL, backspace, etc. encoded.
if (hexdec($match[2]) < 32) {
$string = '\u' . $match[2];
}
else {
$string = mb_convert_encoding(pack('H*', $match[2]), 'UTF-8', 'UCS-2BE');
}
return $match[1] . $string;
};
// To prevent unintentionally unescaping the string "\u", be sure that the
// the backslash itself is not also escaped. Note that because the replacement
// may include part of a 4-digit code for a previous Unicode character, this
// will only escape every-other Unicode character if they are next to each
// other.
$pattern = '/([^\\\\])\\\\u([0-9a-fA-F]{4})/';
$json = preg_replace_callback($pattern, $decode_callback, $json);
// Replace a second time to catch any sequences multiple Unicode characters.
return preg_replace_callback($pattern, $decode_callback, $json);
}