note 101335 added to function.fgetcsv
| From: | Kjeld at osu1 dot php dot net | Date: | Fri, 10 Dec 2010 18:58:00 +0000 |
| Subject: | note 101335 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174781@lists.php.net to get a copy of this message | ||
The fgetcsv() is really great. CSV files are not as simple as they seem. They may span multiple
lines, contain unmatched double quotes (like: 32" screen size) and lot of other surprising
variants. My problem was that I had an ISO-8859-1 encoded file and fgetcsv() expects it to be UTF-8
encoded. I tried with a range of regular expressions I found on the Internet but none of them worked
100 %. So, if you haven't discovered it yet: fgetcsv() is king. My server doesn't have
str_getcsv() so I had to figure out some way to use fgetcsv() for my ISO-8859-1 files.
I found the answer here: http://bugs.php.net/bug.php?id=48507. So I ended up
with:
class utf8encode_filter extends php_user_filter
{
function filter($in, $out, &$consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable($in)) {
$bucket->data = utf8_encode($bucket->data);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
}
and
$fp = @fopen($fileName, "r");
stream_filter_register("utf8encode", "utf8encode_filter")
or die("Failed to register filter");
stream_filter_prepend($fp, "utf8encode");
----
Server IP: 195.184.96.15
Probable Submitter: 94.145.98.179
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- https://master.php.net/note/edit/101335
Del: integrated -- https://master.php.net/note/delete/101335/integrated
Del: useless -- https://master.php.net/note/delete/101335/useless
Del: bad code -- https://master.php.net/note/delete/101335/bad+code
Del: spam -- https://master.php.net/note/delete/101335/spam
Del: non-english -- https://master.php.net/note/delete/101335/non-english
Del: in docs -- https://master.php.net/note/delete/101335/in+docs
Del: other reasons-- https://master.php.net/note/delete/101335
Reject -- https://master.php.net/note/reject/101335
Search -- https://master.php.net/manage/user-notes.php