note 101095 modified in function.json-decode by danbrown

From: Date: Thu, 25 Nov 2010 21:59:56 +0000
Subject: note 101095 modified in function.json-decode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-174361@lists.php.net to get a copy of this message
If you have a json encoded array that contains non UTF8 chars, this function will do the trick: <?php $array = json_decode(safeJSON_chars($iso_8859_1_data)); function safeJSON_chars($data) { $aux = str_split($data); foreach($aux as $a) { $a1 = urlencode($a); $aa = explode("%", $a1); foreach($aa as $v) { if($v!="") { if(hexdec($v)>127) { $data = str_replace($a,"&#".hexdec($v).";",$data); } } } } return $data; } ?> Of course it works if you want to show this inside a HTML page, so entities will be converted. Hope this helps you as much as it helped me. --was-- If you have a json encoded array that contains non UTF8 chars, this function will do the trick: $array = json_decode(safeJSON_chars($iso_8859_1_data)); function safeJSON_chars($data) { $aux = str_split($data); foreach($aux as $a) { $a1 = urlencode($a); $aa = explode("%", $a1); foreach($aa as $v) { if($v!="") { if(hexdec($v)>127) { $data = str_replace($a,"&#".hexdec($v).";",$data); } } } } return $data; } Of course it works if you want to show this inside a HTML page, so entities will be converted. Hope this helps you as much as it helped me. http://php.net/manual/en/function.json-decode.php

« previous php.notes (#174361) next »