Breaking News
Loading...
February 16, 2016

JSON encode utf8 in PHP

9:18 AM
Using json_encode and JSON_UNESCAPED_UNICODE constants
json_encode($response,JSON_UNESCAPED_UNICODE)
Using json_encode, iconv and preg_match_all to bind utf8 string
function json_uencode($data) {
 $json = json_encode($data);
 preg_match_all("/\\\\u([a-f0-9]{4})/i", $json, $all);
 $val = isset($all[1]) ? $all[1] : array();
 foreach($val as $i => $v) {
  $v = iconv('UCS-4LE','UTF-8',pack('V', hexdec('U'.$v)));
  $json = str_replace($all[0][$i], $v, $json);
 }
 return $json;
}

0 comments:

Post a Comment

 
Toggle Footer