PHP use fgetcsv read CSV file garbled solution, _php tutorial

Source: Internet
Author: User

PHP uses Fgetcsv to read the CSV file garbled solution,


The example in this article explains how PHP uses Fgetcsv to read the CSV file garbled. Share to everyone for your reference. The specific analysis is as follows:

Generally speaking in PHP garbled is mostly coding problem, here we analyze the Fgetcsv read CSV file garbled reason and solution.

Examples are as follows:
Copy the Code code as follows: function get_csv_contents ($file _target) {
$handle = fopen ($file _target, ' R ');
while ($data = Fgetcsv ($handle, 1000, ",")) {

$num = count ($data);
echo "

$num Fields $row:
n ";
$row + +;
for ($c =0; $c < $num; $c + +) {
echo $data [$c]. "
n ";;
/*echo getutfstring ($data [$c]) */
}
}
Fclose ($handle);
}
Imported CSV files are saved in ANSI encoding, for the Chinese operating system environment should be GBK encoding, by manually changing the browser character encoding for GBK, garbled situation disappeared, when the following adjustments.

Copy the code as follows: $data = eval (' Return '. Iconv (' GBK ', ' Utf-8 ', Var_export ($data, True)).
$data is an array that needs to convert the encoding.

Add: LINUX fgetcsv read GBK data garbled

When the Linux system is used by the default settings, the Linux server on the GBK CSV format file processing, there will be garbled phenomenon.

The workaround is to:

Use the setlocale function to set environment variables. For example, to set a locale using GB you can use the following statement before Fgetcsv.

Copy the Code code as follows: SetLocale (Lc_all,array (' zh_cn.gbk ', ' zh_cn.gb2312 ', ' zh_cn.gb18030 '));
Use the Linux command locale-a to see which locales are supported by the system.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/908175.html www.bkjia.com true http://www.bkjia.com/PHPjc/908175.html techarticle PHP use fgetcsv read CSV file garbled solution, this example describes the PHP use fgetcsv read CSV file garbled solution. Share to everyone for your reference. Specific ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.