I kept running into the same issue Chris Tyler experienced with lewis [ at t] hcoms [d dot t] co [d dot t] uk's function before realizing that Chris had come up with a solution. However, that solution was just a little off it seems, unless your CSV only contains one line.



If you simply add another --length; in the place you suggested, then the function will always trim the last two characters on the line. Since the newline character is the last character on the line and the redundant quote (or other enclosure) is the second to last character, this works for the final segment. But when parsing segments that do not include a newline character, you end up trimming the redundant enclosure and the last character before the enclosure.



For example,



"he","she","him","her"\r





becomes



[0] => h

[1] => sh

[2] => hi

[3] => her



Since the segment could end with the enclosure (i.e., ") or the enclosure followed by the newline (i.e., "\r

), you have make sure you are only adding another --length; when the latter is the case. Replacing the code block that you suggested with the following will do the trick.



# Is the last thing a newline?

if( $char == $newline )

{

# Well then get rid of it

--$length;

}



# Is the last thing a quote?

if( $trim_quote )

{

# Well then get rid of it

--$length;

}



I've tested this only for the purposes of the script I'm working on at this time. So, there could be other bugs I haven't come across, but this seems like the easiest way to eliminate the redundant enclosure.