note 87428 added to function.split
| From: | GregLevine at osu1 dot php dot net | Date: | Thu, 04 Dec 2008 21:45:33 +0000 |
| Subject: | note 87428 added to function.split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-147644@lists.php.net to get a copy of this message | ||
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\n
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\n), 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.
----
Server IP: 209.41.74.194
Probable Submitter: 74.223.168.50
----
Manual Page -- http://www.php.net/manual/en/function.split.php
Edit -- https://master.php.net/note/edit/87428
Del: integrated -- https://master.php.net/note/delete/87428/integrated
Del: useless -- https://master.php.net/note/delete/87428/useless
Del: bad code -- https://master.php.net/note/delete/87428/bad+code
Del: spam -- https://master.php.net/note/delete/87428/spam
Del: non-english -- https://master.php.net/note/delete/87428/non-english
Del: in docs -- https://master.php.net/note/delete/87428/in+docs
Del: other reasons-- https://master.php.net/note/delete/87428
Reject -- https://master.php.net/note/reject/87428
Search -- https://master.php.net/manage/user-notes.php