如何将带有国际字符的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>
Run Code Online (Sandbox Code Playgroud)
Luk*_*kas 41
json utf8编码和解码:
json_encode($data, JSON_UNESCAPED_UNICODE)
json_decode($json, false, 512, JSON_UNESCAPED_UNICODE)
Run Code Online (Sandbox Code Playgroud)
force utf8也可能有帮助:http://pastebin.com/2XKqYU49
小智 28
header('Content-Type: application/json; charset=utf-8');
Run Code Online (Sandbox Code Playgroud)
Pek*_*ica 24
这是一个编码问题.看起来在某些时候,数据表示为ISO-8859-1.
您的流程的每个部分都需要UTF-8编码.
数据库连接
数据库表
您的PHP文件(如果您在该文件中使用特殊字符,如上例所示)
content-type您输出的标头
Ber*_*Ott 11
如果源文件已经是utf8,则删除utf8_*函数.php5将字符串存储为byte数组.
你应该在html中添加一个用于编码的元标记,你应该添加一个http标头,将转码设置为utf-8.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Run Code Online (Sandbox Code Playgroud)
在PHP中
<?php
header('Content-Type: text/html; charset=utf-8');
Run Code Online (Sandbox Code Playgroud)
$j_decoded = utf8_decode(json_decode($j_encoded)); 编辑
或更正确的$j_encoded = json_encode($j_encoded); $j_decoded = json_decode($j_encoded);不需要编码/解码utf8<meta charset="utf-8" />尝试发送UTF-8字符集标头:
<?php header ('Content-type: text/html; charset=utf-8'); ?>
Run Code Online (Sandbox Code Playgroud)
和HTML meta:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Run Code Online (Sandbox Code Playgroud)