Special PHP JSON Characters

In my manual json type, multiple results should be returned. But I can not output due to special characters. Examples of data in the content array:

Alt n Portakal Film Festivali sonu land.

(Problem: )

 $JSON["today"]=array(); for ($i=0; $i < count($olay_tarih); $i++) { $gelen["date"] = array(); $gelen["content"]=array(); array_push($gelen["date"], $olay_date[$i]); array_push($gelen["content"], $olay_content[$i]); array_push($JSON["today"], $gelen); } echo json_encode($JSON); 
+5
source share
2 answers

Change your code to:

 header('Content-Type: application/json; charset=utf-8', true,200); $JSON["today"]=array(); for ($i=0; $i < count($olay_tarih); $i++) { $gelen["date"]=array(); $gelen["content"]=array(); array_push($gelen["date"], $olay_date[$i]); array_push($gelen["content"], $olay_content[$i]); array_push($JSON["today"], $gelen); } $JSON = array_map('utf8_encode', $JSON); echo json_encode($JSON); 

Adding UTF-8 headers will cause the browser to recognize special characters for this parameter.

+12
source

What is the result with this?

 <?php header('Content-type: text/html; charset=utf-8'); echo(json_encode($JSON, JSON_UNESCAPED_UNICODE)); 

JSON_UNESCAPED_UNICODE

Encodes multibyte Unicode characters literally (selected by default as \uXXXX ). Available since PHP 5.4.0.

+3
source

Source: https://habr.com/ru/post/1238948/


All Articles