dourun2990 2013-06-03 12:39 采纳率: 100%
浏览 37

如果没有机箱字符,你如何使用fgetcsv?

I'm processing csv files from geonames.org and as such I need to get csv lines without any enclosures.

Currently fgetcsv() requires that you provide a single character for the $enclosure parameter and won't process the CSV data if you don't.

How do you retrieve csv data that has no enclosure character using php native functionality?

  • 写回答

2条回答 默认 最新

  • dr897777 2013-06-03 12:48
    关注

    As mentioned, the easiest thing to do is not to use fgetcsv() at all. You can just use the plain fgets() function to read each line from the CSV, and then use explode() to create an array of all the different fields per line

    Here's a small example:

    $handle = fopen("foo.csv", "r");
    
    $i = 1;  // for test output
    
    while (($buffer = fgets($handle)) !== false) {
        $fields = explode(";", $buffer);  // assuming that fields are separated with semicolons
    
        $j = 1;
    
        // Test output
        echo "Line $i: ";
        // This is just for show, you can use a regular for loop instead
        foreach ($fields as $field) {
            echo "Field $j: $field";
            $j++;
        }
    
        echo "
    ";
        $i++;
    }
    
    fclose($handle);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题