I am having trouble JSON-encoding special characters. These characters display normally on my computer, in Notepad, in browsers, and even in my database. However, they do not JSON encode. An example is as follows:
<?
$array['copyright_str'] = "Copyright site.com © 2011-2012";
echo json_encode($array);
?>
The copyright symbol after site.com is what is making the JSON string echo as {"copyright_str":null}
. While this is simple, I have users inputting profile data into a database which can be anything. When one of these funky characters shows up it breaks things. What is a good solution to this issue? The API I coded relies heavily on returning data from the database and printing strings in general as JSON.
My Multibyte settings are as follows:
php -e phpinfo.php | grep mb
Configure Command => './configure' '--enable-bcmath' '--enable-calendar' '--enable-dbase' '--enable-exif' '--enable-ftp' '--enable-gd-native-ttf' '--enable-libxml' '--enable-magic-quotes' '--enable-mbstring' '--enable-pdo=shared' '--enable-sockets' '--enable-zip' '--prefix=/usr/local' '--with-apxs2=/usr/local/apache/bin/apxs' '--with-bz2' '--with-curl=/opt/curlssl/' '--with-curlwrappers' '--with-freetype-dir=/usr' '--with-gd' '--with-imap=/opt/php_with_imap_client/' '--with-imap-ssl=/usr' '--with-jpeg-dir=/usr' '--with-kerberos' '--with-libdir=lib64' '--with-libexpat-dir=/usr' '--with-libxml-dir=/opt/xml2/' '--with-mcrypt=/opt/libmcrypt/' '--with-mhash=/opt/mhash/' '--with-mysql=/usr' '--with-mysql-sock=/var/lib/mysql/mysql.sock' '--with-mysqli=/usr/bin/mysql_config' '--with-openssl=/usr' '--with-openssl-dir=/usr' '--with-pcre-regex=/opt/pcre' '--with-pdo-mysql=shared' '--with-pdo-sqlite=shared' '--with-pic' '--with-png-dir=/usr' '--with-sqlite=shared' '--with-ttf' '--with-xmlrpc' '--with-xpm-dir=/usr' '--with-zlib' '--with-zlib-dir=/usr'
xmlrpc_error_number => 0 => 0
mbstring
Multibyte string engine => libmbfl
mbstring extension makes use of "streamable kanji code filter and converter", which is distributed under the GNU Lesser General Public License version 2.1.
mbstring.detect_order => no value => no value
mbstring.encoding_translation => Off => Off
mbstring.func_overload => 0 => 0
mbstring.http_input => pass => pass
mbstring.http_output => pass => pass
mbstring.internal_encoding => no value => no value
mbstring.language => neutral => neutral
mbstring.strict_detection => Off => Off
mbstring.substitute_character => no value => no value
I'd like to avoid saving things like ©
. Some of this data is going to be stored as plain text.
encode data in UTF-8 format before passing it to json_encode function
<?
$array['copyright_str'] = utf8_encode("Copyright site.com © 2011-2012");
echo json_encode($array);
?>
I'm encoding data with tons of UTF-8 symbols with
json_encode($return, JSON_UNESCAPED_UNICODE)
and it works well. I use it to encode all kinds of languages: Arabic, Chinese, Thai, Lithuanian, German, French, Spanish, etc. All those have different unique symbols. Oh, I haven't tried encoding snowmen ☃ :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With