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 在ISIS中什么是IP从地址
  • ¥15 压测时,并发量过高时,响应时间出现尖刺
  • ¥15 关于vmprotect3.8.4虚拟文件一项
  • ¥15 在不用IT调试的情况下怎样能连外网
  • ¥20 C#调用虚拟键盘TabTip.exe
  • ¥15 Qt4代码实现下面的界面
  • ¥15 CCS离散化传递函数与仿真不一致
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确