Home > Backend Development > PHP Tutorial > Solution to garbled characters when reading csv files using fgetcsv in PHP, _PHP Tutorial

Solution to garbled characters when reading csv files using fgetcsv in PHP, _PHP Tutorial

WBOY
Release: 2016-07-13 10:14:44
Original
949 people have browsed it

Solution to the problem of garbled characters when php uses fgetcsv to read csv files,

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:

Copy code The code is as follows:
function get_csv_contents( $file_target ){
$handle = fopen( $file_target, 'r');
while ($data = fgetcsv($handle, 1000, ",")) {

$num = count($data);
echo "

$num fields in line $row:
n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c]. "
n";;
/*echo getUTFString($data[$c])*/
}
}
fclose($handle);
}


The imported csv file is saved in ansi encoding. For the Chinese operating system environment, the corresponding encoding should be gbk. By manually changing the browser character encoding to gbk, the garbled code disappeared, and the following adjustments were made.

Copy code The code is as follows:
$data = eval('return '.iconv('gbk','utf-8', var_export($data,true)).';');

$data is the array that needs to be converted to encoding.

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.

Copy code The code is as follows:
setlocale(LC_ALL,array('zh_CN.gbk','zh_CN.gb2312','zh_CN. gb18030'));

To determine which locales to use, you can use the Linux command locale -a to check which ones the system supports

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/908175.htmlTechArticlephp uses fgetcsv to read csv files and solves the problem of garbled characters. This article describes the example of php using fgetcsv to read csv files. Solution to garbled characters. Share it with everyone for your reference. Specifically...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template