note 84828 added to function.fgetcsv
| From: | Dave at osu1 dot php dot net | Date: | Fri, 01 Aug 2008 00:03:07 +0000 |
| Subject: | note 84828 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-142994@lists.php.net to get a copy of this message | ||
If you want to get your CSV rows (in this case a file posted from a form) into one big array, you
can use this code. You may have to play around with the ord() and explode separators depending on
your line endings. You can change to the commented out explode to use " delimiter.
<?php
if( $_FILES['file']['tmp_name'] )
{
$csv_rows=Array();
$csv="";
$handle = fopen($_FILES['file']['tmp_name'], "r");
//load char by char, to replace line endings
while($data = fgetc($handle))
{
if(ord($data)==13)
{
$csv.="\r\n";
}
else
{
$csv.=$data;
}
}
fclose($handle);
$csv_lines=explode("\r\n", $csv);
foreach($csv_lines as $line)
{
$csv_rows[]=explode(",", $line);
//or ltrim(rtrim(explode('","',
$line),'"'),'"') for delimited fields
}
}
?>
----
Server IP: 208.69.120.35
Probable Submitter: 128.223.201.187
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- https://master.php.net/note/edit/84828
Del: integrated -- https://master.php.net/note/delete/84828/integrated
Del: useless -- https://master.php.net/note/delete/84828/useless
Del: bad code -- https://master.php.net/note/delete/84828/bad+code
Del: spam -- https://master.php.net/note/delete/84828/spam
Del: non-english -- https://master.php.net/note/delete/84828/non-english
Del: in docs -- https://master.php.net/note/delete/84828/in+docs
Del: other reasons-- https://master.php.net/note/delete/84828
Reject -- https://master.php.net/note/reject/84828
Search -- https://master.php.net/manage/user-notes.php