fgetcsv skip blank lines in file

前端 未结 3 871
小鲜肉
小鲜肉 2021-01-04 09:26

I have this script that I did, it basically grabs all the files in my \"logs\" folder and merge them all in one array file, my only problem is that, sometimes the script bre

3条回答
  •  臣服心动
    2021-01-04 10:17

    As you can read in the documentation for fgetcsv():

    A blank line in a CSV file will be returned as an array comprising a single null field, and will not be treated as an error.

    Checking for that before adding it to your data array should be sufficient:

    while (($result = fgetcsv($in)) !== false) {
        if (array(null) !== $result) { // ignore blank lines
            $csv[] = $result;
        }
    }
    

提交回复
热议问题