I need to save a UTF-8 text in a file with PHP using this code:
<?php
$filename = "";
if ($handle = opendir("test")) {
while (false !== ($file = readdir($handle))) {
$filename = $file;
}
closedir($handle);
}
$arr = array(
"name" => "اصغر",
"family"=> "فرهادی",
"Movies" => [
"City" => "[جدایی نادر از سیمین]",
"detail" => [
"yearOf" => "2011",
"language" => "persian"
]
]
);
$json = json_encode($arr);
$encoding = mb_detect_encoding($json);
echo $encoding."n";
$json1 = mb_convert_encoding($json, "UTF-8");
$encoding2 = mb_detect_encoding($json1);
echo $encoding2."n";
$h = fopen("out.txt","a+");
fwrite($h, $json1."n");
fclose($h);
I run this code in the console using this command:
php index.php
and output is:
ASCII ASCII
and the content of the output file is:
{“name”:”u0627u0635u063au0631″,”family”:”u0641u0631u0647u0627u062fu06cc”,”Movies”:{“City”:”[u062cu062fu0627u06ccu06cc u0646u0627u062fu0631 u0627u0632 u0633u06ccu0645u06ccu0646]”,”detail”:{“yearOf”:”2011″,”language”:”persian”}}}
but I need to save the result in the file in Persian transcript. I also checked the iconv
function, but the result was the same.