dongzongzi0379 2013-11-12 14:37
浏览 29
已采纳

CSV未在新行上正确解析

I have a CSV file that looks like:

Header1,Header2,Header3
value1,value2,value3
value11,value12,value13
value21,value22,value23

etc...

When I use this code to parse through it:

$csvData = file_get_contents($file_url);
$lines = explode(" ", $csvData);
$array = array();
foreach ($lines as $line) {
    $array[] = str_getcsv($line);
}
dpm($array);

the first value of a line always ends up being part of the end of the last header: enter image description here

EDIT: There must have been an issue with cache as this is working fine.

  • 写回答

3条回答 默认 最新

  • dongxing1960 2013-11-12 14:39
    关注

    For reading CSV file explode() and file_get_contents() are not the correct options. Go for fgetcsv You can do like below for reading CSV file:

    <?php
    $row = 1;
    if (($handle = fopen("test.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            echo "<p> $num fields in line $row: <br /></p>
    ";
            $row++;
            for ($c=0; $c < $num; $c++) {
                echo $data[$c] . "<br />
    ";
            }
        }
        fclose($handle);
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入