无法将阿拉伯语文本从 csv 保存到 txt 文件


Can't save Arabic text from csv to txt file

我有一个这样的csv文件:

region_name, city_name, district_name, ppm_value
منطقة, مدينة , حي , 220.92
...

所有列都是阿拉伯语的字符串,除了ppm_value浮点数。我正在尝试处理 csv 文件并以 json 格式重现它。我试过这个:

$districts = array();
$fd = fopen('data2.csv', 'r');
while ($row = fgetcsv($fd)) {
    $region = $row[0];
    $city = $row[1];
    $district = new stdClass();
    $district->name = "".$row[2];
    $district->ppm = $row[3];
    //also add region and city to $district
    $districts[] = $district;
}
$json1=json_encode($districts);
file_put_contents("text.txt",$json1);

这将产生以下结果:

[{"name":null,"ppm":"220.92","region":null,"city":null}, ... ]

浮点数已正确保存,但阿拉伯字符串均为空。我什至尝试像这样保存它,但我得到了相同的结果:

$myfile = fopen('text.txt', "w") or die("Unable to open file!");
fwrite($myfile, json_encode($districts));    
fclose($myfile);

我对 php 几乎一无所知,但也许问题是您的阿拉伯语文本被编码为 ASCII?

在将其添加到 JSON 对象之前尝试将其更改为 Unicode,您可能比我更知道如何做到这一点......

string utf8_encode ( string $data )