Home >Backend Development >PHP Tutorial >Solution to garbled characters when reading csv files using fgetcsv in PHP, _PHP Tutorial
The example in this article describes the solution to the problem of garbled characters when reading csv files using fgetcsv in PHP. Share it with everyone for your reference. The specific analysis is as follows:
Generally speaking, encountering garbled characters in PHP is mostly due to encoding problems. Here we analyze the causes and solutions of garbled characters when fgetcsv reads csv files.
Examples are as follows:
$num fields in line $row:
n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c]. "
n";;
/*echo getUTFString($data[$c])*/
}
}
fclose($handle);
}
Supplement: LINUX FGETCSV reads GBK data with garbled characters
When the Linux system uses the default settings, when the gbk csv format file is processed on the Linux server, garbled characters will appear.
The solution is:
Use the setlocale function to set environment variables. For example, to set the regional settings using gb, you can use the following statement before fgetcsv.
I hope this article will be helpful to everyone’s PHP programming design.