function input_csv($handle) { $out = array(); $n = 0; while ($data = fgetcsv_reg($handle, 10000)) { $num = count($data); for ($i = 0; $i < $num; $i++) { $out[$n][$i] = $data[$i]; } $n++; } return $out; }
function &read_csv($file, $limit = 0, $out_charset = 'utf-8') { if (!file_exists($file)) { return false; } #setlocale(LC_ALL, 'zh_CN.gbk'); $fhandle = fopen($file, 'r'); if (!$fhandle) { return false; } $count = 0; while (!feof($fhandle)) { $line_of_text = fgetcsv_reg($fhandle); if (!$line_of_text) { continue; } $result[] = $line_of_text; $count++; if ($limit > 0 && $limit <= $count) { break; } } foreach ($result as &$line) { foreach ($line as &$v) { $v = iconv('gbk', $out_charset, $v); } } fclose($fhandle); return $result; }
function str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = "\\") { setlocale(LC_ALL, 'ja_JP'); $fiveMBs = 5 * 1024 * 1024; $fp = fopen("php://temp/maxmemory:{$fiveMBs}", 'r+'); fputs($fp, $input); rewind($fp); $data = fgetcsv_reg($fp, 1000, $delimiter, $enclosure); // $escape only got added in 5.3.0 fclose($fp); return $data; }