json_encode对Unicode(版权)字符为null的字符串进行编码


json_encode encodes strings with Unicode (copyright) character as null?

我在JSON编码特殊字符时遇到问题。这些字符通常显示在我的电脑、记事本、浏览器甚至数据库中。但是,它们不进行JSON编码。示例如下:

<?
$array['copyright_str'] = "Copyright site.com © 2011-2012";
echo json_encode($array);
?>

site.com之后的版权符号使JSON字符串回显为{"copyright_str":null}。虽然这很简单,但我让用户将配置文件数据输入到数据库中,该数据库可以是任何内容。当这些时髦的角色出现时,一切都会崩溃。什么是解决这个问题的好办法?我编码的API在很大程度上依赖于从数据库返回数据,并通常将字符串打印为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

我想避免保存像&copy;这样的东西。其中一些数据将以纯文本形式存储。

在将数据传递给json_encode函数之前,以UTF-8格式对数据进行编码

<?
    $array['copyright_str'] = utf8_encode("Copyright site.com © 2011-2012");
    echo json_encode($array);
?>

我用用大量UTF-8符号编码数据

json_encode($return, JSON_UNESCAPED_UNICODE)

而且效果很好。我用它来编码各种语言:阿拉伯语、汉语、泰语、立陶宛语、德语、法语、西班牙语等。所有这些语言都有不同的独特符号。哦,我还没试过给雪人编码☃ :)

json_encode 之前使用urlencode

<?
$array['copyright_str'] = "Copyright site.com © 2011-2012";
$array['copyright_str'] = urlencode($array['copyright_str']);
echo json_encode($array);
?>