note 90627 added to function.str-getcsv
| From: | logan at loganleger dot com | Date: | Fri, 01 May 2009 07:31:24 +0000 |
| Subject: | note 90627 added to function.str-getcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154068@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 208.69.120.35
Probable Submitter: 76.165.225.19
----
Manual Page -- http://www.php.net/manual/en/function.str-getcsv.php
Edit -- https://master.php.net/note/edit/90627
Del: integrated -- https://master.php.net/note/delete/90627/integrated
Del: useless -- https://master.php.net/note/delete/90627/useless
Del: bad code -- https://master.php.net/note/delete/90627/bad+code
Del: spam -- https://master.php.net/note/delete/90627/spam
Del: non-english -- https://master.php.net/note/delete/90627/non-english
Del: in docs -- https://master.php.net/note/delete/90627/in+docs
Del: other reasons-- https://master.php.net/note/delete/90627
Reject -- https://master.php.net/note/reject/90627
Search -- https://master.php.net/manage/user-notes.php