Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP JSON Special Characters

Tags:

json

php

encoding

There should return a number of results in my hand json type. But I can not output due to special characters. Example data in 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);
like image 683
Mehmet Özay Avatar asked Dec 22 '15 11:12

Mehmet Özay


1 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 make the browser recognize any special characters for this setting.

like image 137
Phiter Avatar answered Sep 28 '22 07:09

Phiter