note 85965 added to function.fgets
| From: | Simonemailto:c2ltb3BocEBub3R0ZS5jb20= at osu1 dot php dot net | Date: | Fri, 26 Sep 2008 11:39:14 +0000 |
| Subject: | note 85965 added to function.fgets | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-144740@lists.php.net to get a copy of this message | ||
my email above ^^^^^^ should be base64 decoded
fgets and fgetcsv have a lot of problems with unicode files. it's safest to use fread, but the
problem is that since a line hasn't a fixed lenght (in bytes), how to avoid to truncate a line?
this is my solution:
$folder='www.notte.com';
$handle = fopen("/tmp/$folder/file.csv", "r");
$tmpbuffer =''; //to store half lines
while (!feof($handle) || $tmpbuffer) {
$buffer = $tmpbuffer.fread($handle,100*1024);
$buffer=iconv ('unicode','UTF-8',$buffer);
$rows=explode("\n",$buffer);
$countrows=count($rows);
$tmpbuffer='';
if($countrows > 1 ){
$tmpbuffer=iconv ('UTF-8','unicode',$rows[$countrows -1 ]);
unset($rows[$countrows -1 ]);
}
foreach($rows as $row){
echo "\nCORRECT ROW:$row";
//now i can parse this row as a csv
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 87.11.236.7
----
Manual Page -- http://www.php.net/manual/en/function.fgets.php
Edit -- https://master.php.net/note/edit/85965
Del: integrated -- https://master.php.net/note/delete/85965/integrated
Del: useless -- https://master.php.net/note/delete/85965/useless
Del: bad code -- https://master.php.net/note/delete/85965/bad+code
Del: spam -- https://master.php.net/note/delete/85965/spam
Del: non-english -- https://master.php.net/note/delete/85965/non-english
Del: in docs -- https://master.php.net/note/delete/85965/in+docs
Del: other reasons-- https://master.php.net/note/delete/85965
Reject -- https://master.php.net/note/reject/85965
Search -- https://master.php.net/manage/user-notes.php