PHP json_encode json_decode UTF-8 [英] PHP json_encode json_decode UTF-8

查看:123
本文介绍了PHP json_encode json_decode UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将带有国际字符的json编码的字符串保存到数据库,然后在浏览器中解析已解码的字符串?

How can I save a json-encoded string with international characters to the databse and then parse the decoded string in the browser?

<?php           
    $string = "très agréable";  
    // to the database 
    $j_encoded = json_encode(utf8_encode($string)); 
    // get from Database 
    $j_decoded = json_decode($j_encoded); 
?>    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
    <?= $j_decoded ?>
</html> 

推荐答案

这是一个编码问题.看起来在某些时候,数据被表示为ISO-8859-1.

This is an encoding issue. It looks like at some point, the data gets represented as ISO-8859-1.

您的流程的每个部分都需要使用UTF-8编码.

Every part of your process needs to be UTF-8 encoded.

  • 数据库连接

  • The database connection

数据库表

您的PHP文件(如果您在文件中使用特殊字符,如上面的示例所示)

Your PHP file (if you are using special characters inside that file as shown in your example above)

您输出的content-type标头

这篇关于PHP json_encode json_decode UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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