note 119861 deleted from function.fgetcsv by cmb
| From: | cmb@php.net | Date: | Fri, 09 Sep 2016 22:27:35 +0000 |
| Subject: | note 119861 deleted from function.fgetcsv by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-206544@lists.php.net to get a copy of this message | ||
Note Submitter: no spam ryan 10 online at google's mail
----
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.