note 106417 modified in function.json-encode by danbrown
| From: | danbrown@php.net | Date: | Tue, 15 Nov 2011 16:48:32 +0000 |
| Subject: | note 106417 modified in function.json-encode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184501@lists.php.net to get a copy of this message | ||
When you have trouble with json_encode and German umlauts. json_encode converts Strings to NULL when
detecting umlauts not being UTF8encoded.
Here's another recursive UTF8 conversion function and vice-versa. The object handling might be
buggy but works for me.
<?php
function array_utf8_encode_recursive($dat)
{ if (is_string($dat)) {
return utf8_encode($dat);
}
if (is_object($dat)) {
$ovs= get_object_vars($dat);
$new=$dat;
foreach ($ovs as $k =>$v) {
$new->$k=array_utf8_encode_recursive($new->$k);
}
return $new;
}
if (!is_array($dat)) return $dat;
$ret = array();
foreach($dat as $i=>$d) $ret[$i] = array_utf8_encode_recursive($d);
return $ret;
}
function array_utf8_decode_recursive($dat)
{ if (is_string($dat)) {
return utf8_decode($dat);
}
if (is_object($dat)) {
$ovs= get_object_vars($dat);
$new=$dat;
foreach ($ovs as $k =>$v) {
$new->$k=array_utf8_decode_recursive($new->$k);
}
return $new;
}
if (!is_array($dat)) return $dat;
$ret = array();
foreach($dat as $i=>$d) $ret[$i] = array_utf8_decode_recursive($d);
return $ret;
}
?>
--was--
When you have trouble with json_encode and German umlauts. json_encode converts Strings to NULL when
detecting umlauts not being UTF8encoded.
Here's another recursive UTF8 conversion function and vice-versa. The object handling might be
buggy but works for me.
function array_utf8_encode_recursive($dat)
{ if (is_string($dat)) {
return utf8_encode($dat);
}
if (is_object($dat)) {
$ovs= get_object_vars($dat);
$new=$dat;
foreach ($ovs as $k =>$v) {
$new->$k=array_utf8_encode_recursive($new->$k);
}
return $new;
}
if (!is_array($dat)) return $dat;
$ret = array();
foreach($dat as $i=>$d) $ret[$i] = array_utf8_encode_recursive($d);
return $ret;
}
function array_utf8_decode_recursive($dat)
{ if (is_string($dat)) {
return utf8_decode($dat);
}
if (is_object($dat)) {
$ovs= get_object_vars($dat);
$new=$dat;
foreach ($ovs as $k =>$v) {
$new->$k=array_utf8_decode_recursive($new->$k);
}
return $new;
}
if (!is_array($dat)) return $dat;
$ret = array();
foreach($dat as $i=>$d) $ret[$i] = array_utf8_decode_recursive($d);
return $ret;
}
http://php.net/manual/en/function.json-encode.php