首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP json_encode json_decode UTF-8

PHP json_encode json_decode UTF-8
EN

Stack Overflow用户
提问于 2010-11-02 18:59:47
回答 8查看 174.9K关注 0票数 34

如何将带有国际字符的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> 
EN

回答 8

Stack Overflow用户

发布于 2015-05-24 22:55:23

json utf8编码和解码:

json_encode($data, JSON_UNESCAPED_UNICODE)

json_decode($json, false, 512, JSON_UNESCAPED_UNICODE)

强制utf8可能也是有帮助的:http://pastebin.com/2XKqYU49

票数 70
EN

Stack Overflow用户

发布于 2013-12-24 03:45:08

  header('Content-Type: application/json; charset=utf-8');
票数 29
EN

Stack Overflow用户

发布于 2010-11-02 19:17:09

如果您的源文件已经是utf8,那么删除utf8_*函数。php5将字符串存储为字节数组。

您应该在html中添加一个用于编码的meta标记,并且应该添加一个http标头,它将传输编码设置为utf-8。

<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

和php

<?php
header('Content-Type: text/html; charset=utf-8');
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4076988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档