PHP json_encode json_decode UTF-8

标签 php utf-8 json

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

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

最佳答案

json utf8 编码和解码:

json_encode($data, JSON_UNESCAPED_UNICODE)

json_decode($json, false, 512, JSON_UNESCAPED_UNICODE)

强制 utf8 可能有助于:http://pastebin.com/2XKqYU49

关于PHP json_encode json_decode UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4076988/

相关文章:

php - 如何显示弹出窗口,其中包含从 ID 获取数据的表中选择的数据?

php - 删除页面中表的行以及数据库中的记录

c# - FastJSON - 如何使用?

c# - 从 C# 类生成 JSON 模式

PHP/MySQL:修复被隐式 mysqli::set_charset ('latin1' ) 连接损坏的 utf8 文本

python - 解析Json数据到Excel

php - cakephp 3 在记录时使用范围

在多维数组索引/键中使用的php安全字符串

javascript - UTF-8 编码算法如何在 8 位 block 上工作(在 JavaScript 中)?

java - XML 编码 - 使用 UTF-8 格式的 Xstream 将文件中的 XML 文本转换为对象