json_encode将具有Unicode(版权)字符的字符串编码为null? [英] json_encode encodes strings with Unicode (copyright) character as null?

查看:204
本文介绍了json_encode将具有Unicode(版权)字符的字符串编码为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到麻烦JSON编码特殊字符。这些字符正常显示在我的计算机,记事本,浏览器,甚至在我的数据库,但他们不JSON编码。示例如下:

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);
?>

site.com之后的版权符号是使JSON字符串echo为{copyright_str:空值}。虽然这很简单,我有用户输入配置文件数据到一个数据库,可以是任何东西。当这些时髦的人物之一显示它打破了东西。什么是这个问题的好解决方案?我编码的API在很大程度上依赖于从数据库返回数据和打印字符串作为JSON。

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.

编辑:

我的多字节设置如下:

     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

EDIT2:

de避免保存像​​& copy;这样的东西。这些数据中的一些将以UTF-8格式存储为纯文本

I'de like to avoid saving things like "& copy;". Some of this data is going to be stored as plain text

推荐答案

编码数据,然后将其传递给json_encode函数

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);
?>

这篇关于json_encode将具有Unicode(版权)字符的字符串编码为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆