CSV parsing with str_getcsv fails on new line





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















While reading a csv file with PHP a problem occured with a line break within the CSV file. The contents of one cell will be split once a comma is followed by a line break:



$csv = array_map('str_getcsv', file($file));

first,second,"third,
more,text","forth"
next,dataset


This will result in:



1) first | second | third
2) more text | forth
3) next | dataset


While it should result in:



1) first | second | third more text | forth
2) next | dataset


Is this a bug within str_getcsv?










share|improve this question




















  • 1





    No, this is not a bug with str_getcsv(). This is because you're using file() to read the file contents, and it splits the file up by newline. So you're only passing one line at a time to str_getcsv() and it has no possible way of knowing that there's more to come for a particular cell in the next line.

    – rickdenhaan
    Nov 23 '18 at 21:14






  • 1





    If I remember correctly, str_getcsv doesn't allow line breaks within column data, since it assumes that the input string is one single line in the first place.

    – vs97
    Nov 23 '18 at 21:29











  • @vshcherbinin nope, str_getcsv() is fine with line breaks within column data: 3v4l.org/jBL1F

    – rickdenhaan
    Nov 23 '18 at 21:37


















0















While reading a csv file with PHP a problem occured with a line break within the CSV file. The contents of one cell will be split once a comma is followed by a line break:



$csv = array_map('str_getcsv', file($file));

first,second,"third,
more,text","forth"
next,dataset


This will result in:



1) first | second | third
2) more text | forth
3) next | dataset


While it should result in:



1) first | second | third more text | forth
2) next | dataset


Is this a bug within str_getcsv?










share|improve this question




















  • 1





    No, this is not a bug with str_getcsv(). This is because you're using file() to read the file contents, and it splits the file up by newline. So you're only passing one line at a time to str_getcsv() and it has no possible way of knowing that there's more to come for a particular cell in the next line.

    – rickdenhaan
    Nov 23 '18 at 21:14






  • 1





    If I remember correctly, str_getcsv doesn't allow line breaks within column data, since it assumes that the input string is one single line in the first place.

    – vs97
    Nov 23 '18 at 21:29











  • @vshcherbinin nope, str_getcsv() is fine with line breaks within column data: 3v4l.org/jBL1F

    – rickdenhaan
    Nov 23 '18 at 21:37














0












0








0








While reading a csv file with PHP a problem occured with a line break within the CSV file. The contents of one cell will be split once a comma is followed by a line break:



$csv = array_map('str_getcsv', file($file));

first,second,"third,
more,text","forth"
next,dataset


This will result in:



1) first | second | third
2) more text | forth
3) next | dataset


While it should result in:



1) first | second | third more text | forth
2) next | dataset


Is this a bug within str_getcsv?










share|improve this question
















While reading a csv file with PHP a problem occured with a line break within the CSV file. The contents of one cell will be split once a comma is followed by a line break:



$csv = array_map('str_getcsv', file($file));

first,second,"third,
more,text","forth"
next,dataset


This will result in:



1) first | second | third
2) more text | forth
3) next | dataset


While it should result in:



1) first | second | third more text | forth
2) next | dataset


Is this a bug within str_getcsv?







php csv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 0:59









vs97

1,2521322




1,2521322










asked Nov 23 '18 at 21:05









merlinmerlin

83211122




83211122








  • 1





    No, this is not a bug with str_getcsv(). This is because you're using file() to read the file contents, and it splits the file up by newline. So you're only passing one line at a time to str_getcsv() and it has no possible way of knowing that there's more to come for a particular cell in the next line.

    – rickdenhaan
    Nov 23 '18 at 21:14






  • 1





    If I remember correctly, str_getcsv doesn't allow line breaks within column data, since it assumes that the input string is one single line in the first place.

    – vs97
    Nov 23 '18 at 21:29











  • @vshcherbinin nope, str_getcsv() is fine with line breaks within column data: 3v4l.org/jBL1F

    – rickdenhaan
    Nov 23 '18 at 21:37














  • 1





    No, this is not a bug with str_getcsv(). This is because you're using file() to read the file contents, and it splits the file up by newline. So you're only passing one line at a time to str_getcsv() and it has no possible way of knowing that there's more to come for a particular cell in the next line.

    – rickdenhaan
    Nov 23 '18 at 21:14






  • 1





    If I remember correctly, str_getcsv doesn't allow line breaks within column data, since it assumes that the input string is one single line in the first place.

    – vs97
    Nov 23 '18 at 21:29











  • @vshcherbinin nope, str_getcsv() is fine with line breaks within column data: 3v4l.org/jBL1F

    – rickdenhaan
    Nov 23 '18 at 21:37








1




1





No, this is not a bug with str_getcsv(). This is because you're using file() to read the file contents, and it splits the file up by newline. So you're only passing one line at a time to str_getcsv() and it has no possible way of knowing that there's more to come for a particular cell in the next line.

– rickdenhaan
Nov 23 '18 at 21:14





No, this is not a bug with str_getcsv(). This is because you're using file() to read the file contents, and it splits the file up by newline. So you're only passing one line at a time to str_getcsv() and it has no possible way of knowing that there's more to come for a particular cell in the next line.

– rickdenhaan
Nov 23 '18 at 21:14




1




1





If I remember correctly, str_getcsv doesn't allow line breaks within column data, since it assumes that the input string is one single line in the first place.

– vs97
Nov 23 '18 at 21:29





If I remember correctly, str_getcsv doesn't allow line breaks within column data, since it assumes that the input string is one single line in the first place.

– vs97
Nov 23 '18 at 21:29













@vshcherbinin nope, str_getcsv() is fine with line breaks within column data: 3v4l.org/jBL1F

– rickdenhaan
Nov 23 '18 at 21:37





@vshcherbinin nope, str_getcsv() is fine with line breaks within column data: 3v4l.org/jBL1F

– rickdenhaan
Nov 23 '18 at 21:37












1 Answer
1






active

oldest

votes


















0














Don't do that, use fgetcsv(). You're having problems because file() doesn't care about the string encapsulation in your file.



$fh = fopen('file.csv', 'r');

while( $line = fgetcsv($fh) ) {
// do a thing
}

fclose($fh);


https://secure.php.net/manual/en/function.fgetcsv.php



And try not to store all the lines into an array before performing your operations if you can help it. Your system's memory usage will thank you.






share|improve this answer


























  • @NigelRen Works for me: 3v4l.org/552QY

    – Sammitch
    Nov 23 '18 at 22:01













  • OK - not sure why mine didn't work, but too late to think about it :-/

    – Nigel Ren
    Nov 23 '18 at 22:04












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452937%2fcsv-parsing-with-str-getcsv-fails-on-new-line%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Don't do that, use fgetcsv(). You're having problems because file() doesn't care about the string encapsulation in your file.



$fh = fopen('file.csv', 'r');

while( $line = fgetcsv($fh) ) {
// do a thing
}

fclose($fh);


https://secure.php.net/manual/en/function.fgetcsv.php



And try not to store all the lines into an array before performing your operations if you can help it. Your system's memory usage will thank you.






share|improve this answer


























  • @NigelRen Works for me: 3v4l.org/552QY

    – Sammitch
    Nov 23 '18 at 22:01













  • OK - not sure why mine didn't work, but too late to think about it :-/

    – Nigel Ren
    Nov 23 '18 at 22:04
















0














Don't do that, use fgetcsv(). You're having problems because file() doesn't care about the string encapsulation in your file.



$fh = fopen('file.csv', 'r');

while( $line = fgetcsv($fh) ) {
// do a thing
}

fclose($fh);


https://secure.php.net/manual/en/function.fgetcsv.php



And try not to store all the lines into an array before performing your operations if you can help it. Your system's memory usage will thank you.






share|improve this answer


























  • @NigelRen Works for me: 3v4l.org/552QY

    – Sammitch
    Nov 23 '18 at 22:01













  • OK - not sure why mine didn't work, but too late to think about it :-/

    – Nigel Ren
    Nov 23 '18 at 22:04














0












0








0







Don't do that, use fgetcsv(). You're having problems because file() doesn't care about the string encapsulation in your file.



$fh = fopen('file.csv', 'r');

while( $line = fgetcsv($fh) ) {
// do a thing
}

fclose($fh);


https://secure.php.net/manual/en/function.fgetcsv.php



And try not to store all the lines into an array before performing your operations if you can help it. Your system's memory usage will thank you.






share|improve this answer















Don't do that, use fgetcsv(). You're having problems because file() doesn't care about the string encapsulation in your file.



$fh = fopen('file.csv', 'r');

while( $line = fgetcsv($fh) ) {
// do a thing
}

fclose($fh);


https://secure.php.net/manual/en/function.fgetcsv.php



And try not to store all the lines into an array before performing your operations if you can help it. Your system's memory usage will thank you.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 22:15









miken32

24.7k95173




24.7k95173










answered Nov 23 '18 at 21:55









SammitchSammitch

20.5k42965




20.5k42965













  • @NigelRen Works for me: 3v4l.org/552QY

    – Sammitch
    Nov 23 '18 at 22:01













  • OK - not sure why mine didn't work, but too late to think about it :-/

    – Nigel Ren
    Nov 23 '18 at 22:04



















  • @NigelRen Works for me: 3v4l.org/552QY

    – Sammitch
    Nov 23 '18 at 22:01













  • OK - not sure why mine didn't work, but too late to think about it :-/

    – Nigel Ren
    Nov 23 '18 at 22:04

















@NigelRen Works for me: 3v4l.org/552QY

– Sammitch
Nov 23 '18 at 22:01







@NigelRen Works for me: 3v4l.org/552QY

– Sammitch
Nov 23 '18 at 22:01















OK - not sure why mine didn't work, but too late to think about it :-/

– Nigel Ren
Nov 23 '18 at 22:04





OK - not sure why mine didn't work, but too late to think about it :-/

– Nigel Ren
Nov 23 '18 at 22:04




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452937%2fcsv-parsing-with-str-getcsv-fails-on-new-line%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()