note 107911 added to function.fgetcsv
| From: | michael dot arnauts at gmail dot com | Date: | Wed, 14 Mar 2012 13:19:18 +0000 |
| Subject: | note 107911 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186657@lists.php.net to get a copy of this message | ||
fgetcsv seems to handle newlines within fields fine. So in fact it is not reading a line, but keeps
reading untill it finds a \n-character that's not quoted as a field.
Example:
<?php
/* test.csv contains:
"col 1","col2","col3"
"this
is
having
multiple
lines","this not","this also not"
"normal record","nothing to see here","no data"
*/
$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle)) !== FALSE) {
var_dump($data);
}
?>
Returns:
array(3) {
[0]=>
string(5) "col 1"
[1]=>
string(4) "col2"
[2]=>
string(4) "col3"
}
array(3) {
[0]=>
string(29) "this
is
having
multiple
lines"
[1]=>
string(8) "this not"
[2]=>
string(13) "this also not"
}
array(3) {
[0]=>
string(13) "normal record"
[1]=>
string(19) "nothing to see here"
[2]=>
string(7) "no data"
}
This means that you can expect fgetcsv to handle newlines within fields fine. This was not clear
from the documentation.
----
Server IP: 69.147.83.197
Probable Submitter: 188.118.4.210
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- https://master.php.net/note/edit/107911
Del: integrated -- https://master.php.net/note/delete/107911/integrated
Del: useless -- https://master.php.net/note/delete/107911/useless
Del: bad code -- https://master.php.net/note/delete/107911/bad+code
Del: spam -- https://master.php.net/note/delete/107911/spam
Del: non-english -- https://master.php.net/note/delete/107911/non-english
Del: in docs -- https://master.php.net/note/delete/107911/in+docs
Del: other reasons-- https://master.php.net/note/delete/107911
Reject -- https://master.php.net/note/reject/107911
Search -- https://master.php.net/manage/user-notes.php