note 90627 deleted from function.str-getcsv by danbrown
| From: | danbrown@php.net | Date: | Fri, 01 May 2009 12:01:10 +0000 |
| Subject: | note 90627 deleted from function.str-getcsv by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154070@lists.php.net to get a copy of this message | ||
Note Submitter: logan at loganleger dot com
----
I modified keananda's function to use cURL instead.
<?php
function parse_csv($file, $options = null) {
$delimiter = empty($options['delimiter']) ? "," :
$options['delimiter'];
$to_object = empty($options['to_object']) ? false : true;
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$str = curl_exec($ch);
curl_close($ch);
$lines = explode("\n", $str);
$field_names = explode($delimiter, array_shift($lines));
foreach ($lines as $line) {
// Skip the empty line
if (empty($line)) continue;
$fields = explode($delimiter, $line);
$_res = $to_object ? new stdClass : array();
foreach ($field_names as $key => $f) {
if ($to_object) {
$_res->{$f} = $fields[$key];
} else {
$_res[$f] = $fields[$key];
}
}
$res[] = $_res;
}
return $res;
}
?>
Otherwise, still works the same.