note 100492 modified in function.json-encode by danbrown
| From: | danbrown@php.net | Date: | Tue, 19 Oct 2010 14:56:49 +0000 |
| Subject: | note 100492 modified in function.json-encode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173284@lists.php.net to get a copy of this message | ||
Because json_encode() only deals with utf8, it is often necessary to convert all the string values
inside an array to utf8. I've created these two functions:
<?php
function utf8_encode_all($dat) // -- It returns $dat encoded to UTF8
{
if (is_string($dat)) return utf8_encode($dat);
if (!is_array($dat)) return $dat;
$ret = array();
foreach($dat as $i=>$d) $ret[$i] = utf8_encode_all($d);
return $ret;
}
/* ....... */
function utf8_decode_all($dat) // -- It returns $dat decoded from UTF8
{
if (is_string($dat)) return utf8_decode($dat);
if (!is_array($dat)) return $dat;
$ret = array();
foreach($dat as $i=>$d) $ret[$i] = utf8_decode_all($d);
return $ret;
}
/* ....... */
?>
--was--
Because json_encode() only deals with utf8, it is often necessary to convert all the string values
inside an array to utf8. I've created these two functions:
function utf8_encode_all($dat) // -- It returns $dat encoded to UTF8
{
if (is_string($dat)) return utf8_encode($dat);
if (!is_array($dat)) return $dat;
$ret = array();
foreach($dat as $i=>$d) $ret[$i] = utf8_encode_all($d);
return $ret;
}
/* ....... */
function utf8_decode_all($dat) // -- It returns $dat decoded from UTF8
{
if (is_string($dat)) return utf8_decode($dat);
if (!is_array($dat)) return $dat;
$ret = array();
foreach($dat as $i=>$d) $ret[$i] = utf8_decode_all($d);
return $ret;
}
/* ....... */
http://php.net/manual/en/function.json-encode.php