在php中读取csv文件

发布于 2024-09-15 21:37:10 字数 437 浏览 2 评论 0原文

我想逐行读取 csv 文件。我已经编写了代码,但它以加密方式给出输出。我的代码是

$row = 1;
$handle = fopen($_FILES['csv']['tmp_name'], "r");
 while (($data = fgetcsv($handle, 1000,",")) != FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);

I want to read a csv file line by line. i have written code but it give output in encrypted manner . my code is

$row = 1;
$handle = fopen($_FILES['csv']['tmp_name'], "r");
 while (($data = fgetcsv($handle, 1000,",")) != FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合约呢 2024-09-22 21:37:10

在这里,我提交一些示例代码来从 csv 文件读取数据。

// Set path to CSV file
$csvFile = 'test.csv';
$file_handle = fopen($csvFile, 'r');
//fgetcsv($file_handle);//skips the first line while reading the csv file, uncomment this line if you want to skip the first line in csv file
while (!feof($file_handle) )
{
    $csv_data[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);

/*echo '<pre>';
print_r($csv_data);
echo '</pre>';*/

foreach($csv_data as $data)
{
    echo "<br>column 1 data = ".$data[0];
    echo "<br>column 2 data = ".$data[1];
}

该代码应该适合您,即使在使用该代码之后,如果您仍然面临加密文本的问题,那么我认为文档字符编码格式会出现一些问题。

here I am submitting some sample code to read data from a csv file.

// Set path to CSV file
$csvFile = 'test.csv';
$file_handle = fopen($csvFile, 'r');
//fgetcsv($file_handle);//skips the first line while reading the csv file, uncomment this line if you want to skip the first line in csv file
while (!feof($file_handle) )
{
    $csv_data[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);

/*echo '<pre>';
print_r($csv_data);
echo '</pre>';*/

foreach($csv_data as $data)
{
    echo "<br>column 1 data = ".$data[0];
    echo "<br>column 2 data = ".$data[1];
}

This code should work for you, even after using this code if still you face problem with encrypted text then I think there would be some problem with document character encoding format.

め七分饶幸 2024-09-22 21:37:10

这与 PHP fgetcsv() 文档中的示例 #1 基本完全相同。检查您的输入文件,因为这里实际上没有任何内容可以“加密”数据。如果文件采用外来字符集编码,并且您在 ISO-8859 上输出,那么您将获得“加密”输出。

This is basically exactly the same as example #1 in the PHP fgetcsv() docs. Check your input file, as there's nothing really in here that would "encrypt" data. If the file's encoded in a foreign character set, and you're outputting on ISO-8859, then you will get "encrypted" output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文