PHP json_encode json_decode utf - 8

时间:2022-02-04 11:16:45

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

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

8 个解决方案

#1


21  

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

这是一个编码问题。在某个点上,数据被表示为ISO-8859-1。

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

过程的每个部分都需要UTF-8编码。

  • The database connection

    数据库连接

  • The database tables

    数据库表

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

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

  • The content-type headers that you output

    您输出的内容类型标头。

#2


27  

json utf8 encode and decode:

json utf8编码和解码:

json_encode($data, JSON_UNESCAPED_UNICODE)

json_decode($json, false, 512, JSON_UNESCAPED_UNICODE)

force utf8 might be helpfull too: http://pastebin.com/2XKqYU49

force utf8可能也有帮助:http://pastebin.com/2XKqYU49。

#3


25  

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

#4


11  

If your source-file is already utf8 then drop the utf8_* functions. php5 is storing strings as array of byte.

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

you should add a meta tag for encoding within the html AND you should add an http header which sets the transferencoding to utf-8.

您应该在html中添加一个用于编码的元标记,您应该添加一个http头,它将转换编码设置为utf-8。

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

and in php

在php中,

<?php
header('Content-Type: text/html; charset=utf-8');

#5


4  

Try sending the UTF-8 Charset header:

尝试发送UTF-8字符集头:

<?php header ('Content-type: text/html; charset=utf-8'); ?>

And the HTML meta:

和HTML元:

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

#6


3  

  1. utf8_decode $j_decoded = utf8_decode(json_decode($j_encoded)); EDIT or to be more correct $j_encoded = json_encode($j_encoded); $j_decoded = json_decode($j_encoded); no need for en/decoding utf8
  2. utf8_decode $ j_decoded = utf8_decode(json_decode(j_encoded美元));编辑或更正确的是$j_encoded = json_encode($j_encoded);j_encoded美元$ j_decoded = json_decode();不需要en/解码utf8。
  3. <meta charset="utf-8" />
  4. < meta charset = " utf - 8 " / >

#7


0  

if you get "unexpected Character" error you should check if there is a BOM (Byte Order Marker saved into your utf-8 json. You can either remove the first character or save if without BOM.

如果出现“意外字符”错误,应该检查是否存在BOM(将字节顺序标记保存到utf-8 json中)。如果没有BOM,您可以删除第一个字符或保存。

#8


0  

Work for me :)

为我工作:)

function jsonEncodeArray( $array ){
    array_walk_recursive( $array, function(&$item) { 
       $item = utf8_encode( $item ); 
    });
    return json_encode( $array );
}

#1


21  

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

这是一个编码问题。在某个点上,数据被表示为ISO-8859-1。

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

过程的每个部分都需要UTF-8编码。

  • The database connection

    数据库连接

  • The database tables

    数据库表

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

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

  • The content-type headers that you output

    您输出的内容类型标头。

#2


27  

json utf8 encode and decode:

json utf8编码和解码:

json_encode($data, JSON_UNESCAPED_UNICODE)

json_decode($json, false, 512, JSON_UNESCAPED_UNICODE)

force utf8 might be helpfull too: http://pastebin.com/2XKqYU49

force utf8可能也有帮助:http://pastebin.com/2XKqYU49。

#3


25  

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

#4


11  

If your source-file is already utf8 then drop the utf8_* functions. php5 is storing strings as array of byte.

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

you should add a meta tag for encoding within the html AND you should add an http header which sets the transferencoding to utf-8.

您应该在html中添加一个用于编码的元标记,您应该添加一个http头,它将转换编码设置为utf-8。

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

and in php

在php中,

<?php
header('Content-Type: text/html; charset=utf-8');

#5


4  

Try sending the UTF-8 Charset header:

尝试发送UTF-8字符集头:

<?php header ('Content-type: text/html; charset=utf-8'); ?>

And the HTML meta:

和HTML元:

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

#6


3  

  1. utf8_decode $j_decoded = utf8_decode(json_decode($j_encoded)); EDIT or to be more correct $j_encoded = json_encode($j_encoded); $j_decoded = json_decode($j_encoded); no need for en/decoding utf8
  2. utf8_decode $ j_decoded = utf8_decode(json_decode(j_encoded美元));编辑或更正确的是$j_encoded = json_encode($j_encoded);j_encoded美元$ j_decoded = json_decode();不需要en/解码utf8。
  3. <meta charset="utf-8" />
  4. < meta charset = " utf - 8 " / >

#7


0  

if you get "unexpected Character" error you should check if there is a BOM (Byte Order Marker saved into your utf-8 json. You can either remove the first character or save if without BOM.

如果出现“意外字符”错误,应该检查是否存在BOM(将字节顺序标记保存到utf-8 json中)。如果没有BOM,您可以删除第一个字符或保存。

#8


0  

Work for me :)

为我工作:)

function jsonEncodeArray( $array ){
    array_walk_recursive( $array, function(&$item) { 
       $item = utf8_encode( $item ); 
    });
    return json_encode( $array );
}