Home >Backend Development >PHP Problem >How to solve the problem of garbled characters when importing csv into php

How to solve the problem of garbled characters when importing csv into php

PHPz
PHPzOriginal
2023-03-29 10:12:441149browse

During the process of CSV import in php, sometimes the imported CSV file will encounter the problem of garbled characters, resulting in errors in the imported data. So how to solve the problem of CSV importing garbled characters? Here are some solutions.

1. Set the CSV file encoding format

First of all, the encoding format of the imported CSV file should be consistent with the encoding format of the PHP file. You can select the appropriate encoding when selecting "Save As" in Excel. format to save. Or you can use a text editor such as Notepad to open the CSV file, convert the encoding format to UTF-8 or GBK, and save the file.

For example, to import a GBK-encoded CSV file into a UTF-8 encoded PHP file, you can add the following content in the PHP code:

if ($_FILES['file']['name']) {
    $filename = $_FILES['file']['name']; //获取文件名
    if ($filename) {
        $type = $_FILES['file']['type'];
        $tmp_name = $_FILES['file']['tmp_name'];
        $size = $_FILES['file']['size'];
        $fp = fopen($tmp_name, "r"); //将文件读取成字符串
        $content = fread($fp, $size);
        $content = iconv("GBK", "UTF-8", $content); //将GBK编码转换为UTF-8编码
        fclose($fp);
    }
}

2. Use the fgetcsv function

Use the fgetcsv function to read the contents of the imported CSV file and then process it accordingly. The fgetcsv function will read the contents of a line of CSV file and store it in an array.

For example:

if ($_FILES['file']['name']) {
    $filename = $_FILES['file']['name']; //获取文件名
    if ($filename) {
        $type = $_FILES['file']['type'];
        $tmp_name = $_FILES['file']['tmp_name'];
        $size = $_FILES['file']['size'];

        $fp = fopen($tmp_name, "r"); //将文件读取成字符串

        //使用fgetcsv读取csv文件
        while($line = fgetcsv($fp)) {
            if($line!=null){//如果读到的行不为空,进行以下操作
                $user[$j][0] = iconv("GBK", "UTF-8",$line[0]);//将从csv文件中读道的转换成utf-8
                $user[$j][1] = iconv("GBK", "UTF-8",$line[1]);
                $j++;
            }
        }

        fclose($fp);
    }
}

3. Use the mb_convert_encoding function

Use the mb_convert_encoding function to convert the encoding format of the CSV file to UTF-8.

For example:

if ($_FILES['file']['name']) {
    $filename = $_FILES['file']['name']; //获取文件名
    if ($filename) {
        $type = $_FILES['file']['type'];
        $tmp_name = $_FILES['file']['tmp_name'];
        $size = $_FILES['file']['size'];
        $fp = fopen($tmp_name, "r"); //将文件读取成字符串

        while($line = fgets($fp)){
            $buffer[] = mb_convert_encoding($line, 'UTF-8', 'GBK');//将csv文件内容的编码格式转化为UTF-8
        }

        fclose($fp);
    }
}

Summary:

The above are several methods to solve the problem of garbled characters imported into PHP CSV. Different methods have certain applicability to different garbled characters problems. , you can choose a method that suits you. Of course, you also need to pay attention to other issues during the import process of CSV files, such as row separators, column separators, etc., to ensure that the imported data is complete and correct.

The above is the detailed content of How to solve the problem of garbled characters when importing csv into php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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