카테고리 없음

[PHP] PHP json_encode json_decode UTF-8

필살기쓰세요 2021. 2. 15. 22:48

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

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

  • The database connection

  • The database tables

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

  • The content-type headers that you output

-------------------

json utf8 encode and decode:

json_encode($data, JSON_UNESCAPED_UNICODE)

json_decode($json, false, 512, JSON_UNESCAPED_UNICODE)

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

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

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

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>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   

and in php

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

Try sending the UTF-8 Charset header:

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

And the HTML meta:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-------------------
  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. <meta charset="utf-8" />
-------------------

나를 위해 두 가지 방법

<?php

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

echo json_encode($YourData, \JSON_UNESCAPED_UNICODE);
-------------------

"예기치 않은 문자"오류가 발생하면 BOM (UTF-8 json에 저장된 바이트 순서 마커)이 있는지 확인해야합니다. 첫 번째 문자를 제거하거나 BOM이없는 경우 저장할 수 있습니다.

-------------------

나를 위해 일하십시오 :)

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

나는 같은 문제가 있었다. 데이터를 db에 넣는 방법에 따라 다를 수 있지만 나를 위해 일한 것을 시도하십시오.

$str = json_encode($data);
$str = addslashes($str);

데이터를 db에 저장하기 전에이 작업을 수행하십시오.



출처
https://stackoverflow.com/questions/39939984