note 39626 deleted from function.fgetcsv by cmb

From: Date: Tue, 09 Oct 2018 12:18:29 +0000
Subject: note 39626 deleted from function.fgetcsv by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-209405@lists.php.net to get a copy of this message
Note Submitter: Dave Meiners ---- 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); ?>

« previous php.notes (#209405) next »