note 39626 added to function.fgetcsv

From: Date: Thu, 05 Feb 2004 01:37:49 +0000
Subject: note 39626 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-64660@lists.php.net to get a copy of this message
using the example above with a length of 1000 will truncate any csv rows longer than 1000 bytes, the remainder of that line will be represented next time you call $data = fgetcsv(). one solution i have seen to this is to use filesize("test.csv") as the length argument, however sometimes with large csv files you may encounter errors for exceeding the memory limit. to remedy this, i have read the csv file into an array, looping through that array to find the longest line in the csv, and then using that value as my length argument, unset($array) to free up the memory. im open to better solutions. <?php $length = 1000; $array = file("test.csv"); for($i=0;$i<count($array);$i++) { if ($length < strlen($array[$i])) { $length = strlen($array[$i]); } } unset($array); $handle = fopen("test.csv", "r"); while ($data = fgetcsv($handle, $length, ",")) { // do what you want with your array here. } fclose($handle); ?> ---- Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39626 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39626&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39626&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#64660) next »