note 119861 added to function.fgetcsv

From: Date: Wed, 07 Sep 2016 21:57:39 +0000
Subject: note 119861 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-206522@lists.php.net to get a copy of this message
It appears as though the optional $length parameter is not a hard cut-off for how many characters will be read from the file as I would have assumed; rather the function will continue to read from the CSV file until a newline (that isn't enclosed) is reached or the given $length is reached UNLESS that length is exceeded while partway through an enclosed element (in quotes). FOR EXAMPLE A CSV file containing the 2 lines: "really really really really really really really long enclosed string" AND reallyreallyreallyreallyreallyreallyreallylongstringnotenclosed After using fgetcsv to read the 2 lines with a 20 character limit: $arr = array(); $f = fopen("filename", "r"); while ($line = fgetcsv($f, 20)) $arr[] = $line; echo "<pre>"; var_dump($arr); echo "</pre>"; You will get the following output: array(5) { [0]=> array(1) { [0]=> string(69) "really really really really really really really long enclosed string" } [1]=> array(1) { [0]=> string(20) "reallyreallyreallyre" } [2]=> array(1) { [0]=> string(20) "allyreallyreallyreal" } [3]=> array(1) { [0]=> string(20) "lylongstringnotenclo" } [4]=> array(1) { [0]=> string(3) "sed" } } So as you can see, the limit is effectively ignored for lines that contain enclosed elements. Don't know if this is a feature but if so, it should certainly be documented. ---- Server IP: 72.52.91.14 Probable Submitter: 208.72.125.2 ---- Manual Page -- http://php.net/manual/en/function.fgetcsv.php Edit -- https://master.php.net/note/edit/119861 Del: integrated -- https://master.php.net/note/delete/119861/integrated Del: useless -- https://master.php.net/note/delete/119861/useless Del: bad code -- https://master.php.net/note/delete/119861/bad+code Del: spam -- https://master.php.net/note/delete/119861/spam Del: non-english -- https://master.php.net/note/delete/119861/non-english Del: in docs -- https://master.php.net/note/delete/119861/in+docs Del: other reasons-- https://master.php.net/note/delete/119861 Reject -- https://master.php.net/note/reject/119861 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#206522) next »