March 30, 2012

eZend_Json::encode or json_encode with special characters

Question by user1252306

I’m trying to encode an array which has special characters such as ñ. When I do that it returns null. I tried to use this option in Zend:

Zend_Json::$useBuiltinEncoderDecoder = true: 

And it doesn’t return null but, for example, in this string “something with ñ” return “”something with u00f1”

And if a I use for json_encode, for example:

$string = "something with ñ";
echo "<pre>";
$encode_json = json_encode($string) ;

return is the same:

something with ñ

something with u00f1

And if I use utf8_enconde(), return this character u00c3 for ñ.

Some ideas to solve my problem? I need to save in the database at list words with ñ, if I can save another special character would be great.

Answer by Starx

You can encode Unicode Characters using json_encode() function as well

echo json_encode($yourdata, JSON_UNESCAPED_UNICODE);

Note: This is only available PHP 5.4 onwards

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!