note 84828 deleted from function.fgetcsv by cmb
| From: | cmb@php.net | Date: | Sun, 16 Jun 2019 08:09:03 +0000 |
| Subject: | note 84828 deleted from function.fgetcsv by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211266@lists.php.net to get a copy of this message | ||
Note Submitter: Dave
----
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
}
}
?>