Hi all:
We noticed yesterday on the IRC chat (zftalk) that there is an issue with
Zend_Json::encode:
$test = array("test à test");
$test2 = Zend_Json::encode($test);
print_r($test);
print('<br>Encoded array: ' . $test2);
$test2 contains only 'test '. This was done on PHP 5.2.1. Checking the
source code revealed that the encode function uses json_encode if existent.
Looking in the php docs (
http://ca3.php.net/manual/en/function.json-encode.php) I found the following
comment:
<snip>
Take care that json_encode() expects strings to be encoded to be in UTF8
format, while by default PHP strings are ISO-8859-1 encoded.
This means that
json_encode(array('àü'));
will produce a json representation of an empty string, while
json_encode(array(utf8_encode('àü')));
will work.
The same applies to decoding, too, of course...
</snip>
Using the following code worked for Zend_Json::encode:
$test = array("test à test");
Zend_Json::$useBuiltinEncoderDecoder = true;
$test2 = Zend_Json::encode($test);
print_r($test);
print('<br>Encoded array: ' . $test2);
Not 100% certain if there should be an auto detection for the encoding or if
it should be the developers responsibility to pass in utf8 strings into the
encode function. According to the guy who had the issue, it worked on one
PHP version with json_encode but not on another.
Any thoughts?
Gunter