📄 csv.php
字号:
// Insert last field value. $ret[] = File_CSV::unquote($buff, $quote); // Pair the array elements to fields count. return array_merge($ret, array_fill(count($ret), ($f - 1) - (count($ret) - 1), '') ); } if ($prev == "\r") { $buff = substr($buff, 0, -1); } // Convert EOL character to Unix EOL (LF). if ($eol2unix) { $buff = preg_replace('/(\r\n|\r)$/', "\n", $buff); } $ret[] = File_CSV::unquote($buff, $quote); if (count($ret) == $f) { return $ret; } $buff = ''; $i++; continue; } $buff .= $c; } return !feof($fp) ? $ret : false; } /** * Reads a "row" from a CSV file and return it as an array * * @param string $file The CSV file * @param array &$conf The configuration of the dest CSV * * @return mixed Array or false */ function read($file, &$conf) { if (!$fp = File_CSV::getPointer($file, $conf, FILE_MODE_READ)) { return false; } // The size is limited to 4K if (!$line = fgets($fp, 4096)) { return false; } $fields = $conf['fields'] == 1 ? array($line) : explode($conf['sep'], $line); if ($conf['quote']) { $last =& $fields[count($fields) - 1]; // Fallback to read the line with readQuoted when guess // that the simple explode won't work right if (($last{strlen($last) - 1} == "\n" && $last{0} == $conf['quote'] && $last{strlen(rtrim($last)) - 1} != $conf['quote']) || (count($fields) != $conf['fields']) // XXX perhaps there is a separator inside a quoted field //preg_match("|{$conf['quote']}.*{$conf['sep']}.*{$conf['quote']}|U", $line) ) { fseek($fp, -1 * strlen($line), SEEK_CUR); return File_CSV::readQuoted($file, $conf); } else { $last = rtrim($last); foreach ($fields as $k => $v) { $fields[$k] = File_CSV::unquote($v, $conf['quote']); } } } if (count($fields) != $conf['fields']) { File_CSV::raiseError("Read wrong fields number count: '". count($fields) . "' expected ".$conf['fields']); return true; } return $fields; } /** * Internal use only, will be removed in the future * * @param string $str The string to debug * @access private */ function _dbgBuff($str) { if (strpos($str, "\r") !== false) { $str = str_replace("\r", "_r_", $str); } if (strpos($str, "\n") !== false) { $str = str_replace("\n", "_n_", $str); } if (strpos($str, "\t") !== false) { $str = str_replace("\t", "_t_", $str); } echo "buff: ($str)\n"; } /** * Writes a struc (array) in a file as CSV * * @param string $file The filename where to write the data * @param array $fields Ordered array with the data * @param array &$conf The configuration of the dest CSV * * @return bool True on success false otherwise */ function write($file, $fields, &$conf) { if (!$fp = File_CSV::getPointer($file, $conf, FILE_MODE_WRITE)) { return false; } if (count($fields) != $conf['fields']) { File_CSV::raiseError("Wrong fields number count: '". count($fields) . "' expected ".$conf['fields']); return true; } $write = ''; for ($i = 0; $i < count($fields); $i++) { if (!is_numeric($fields[$i]) && $conf['quote']) { $write .= $conf['quote'] . $fields[$i] . $conf['quote']; } else { $write .= $fields[$i]; } if ($i < (count($fields) - 1)) { $write .= $conf['sep']; } else { $write .= $conf['crlf']; } } if (!fwrite($fp, $write)) { return File_CSV::raiseError('Can not write to file'); } return true; } /** * Discover the format of a CSV file (the number of fields, the separator * and if it quote string fields) * * @param string the CSV file name * @param array extra separators that should be checked for. * @return mixed Assoc array or false */ function discoverFormat($file, $extraSeps = array()) { if (!$fp = @fopen($file, 'r')) { return File_CSV::raiseError("Could not open file: $file"); } $seps = array("\t", ';', ':', ','); $seps = array_merge($seps, $extraSeps); $matches = array(); // Set auto detect line ending for Mac EOL support if < PHP 4.3.0. $phpver = version_compare('4.3.0', phpversion(), '<'); if ($phpver) { $oldini = ini_get('auto_detect_line_endings'); ini_set('auto_detect_line_endings', '1'); } // Take the first 10 lines and store the number of ocurrences // for each separator in each line $lines = file($file); if (count($lines) > 10) { $lines = array_slice($lines, 0, 10); } if ($phpver) { ini_set('auto_detect_line_endings', $oldini); } foreach ($lines as $line) { foreach ($seps as $sep) { $matches[$sep][] = substr_count($line, $sep); } } $final = array(); // Group the results by amount of equal ocurrences foreach ($matches as $sep => $res) { $times = array(); $times[0] = 0; foreach ($res as $k => $num) { if ($num > 0) { $times[$num] = (isset($times[$num])) ? $times[$num] + 1 : 1; } } arsort($times); // Use max fields count. $fields[$sep] = max(array_flip($times)); $amount[$sep] = $times[key($times)]; } arsort($amount); $sep = key($amount); $conf['fields'] = $fields[$sep] + 1; $conf['sep'] = $sep; // Test if there are fields with quotes arround in the first 5 lines $quotes = '"\''; $quote = null; if (count($lines) > 5) { $lines = array_slice($lines, 0, 5); } foreach ($lines as $line) { if (preg_match("|$sep([$quotes]).*([$quotes])$sep|U", $line, $match)) { if ($match[1] == $match[2]) { $quote = $match[1]; break; } } if (preg_match("|^([$quotes]).*([$quotes])$sep{0,1}|", $line, $match) || preg_match("|([$quotes]).*([$quotes])$sep\s$|Us", $line, $match)) { if ($match[1] == $match[2]) { $quote = $match[1]; break; } } } $conf['quote'] = $quote; fclose($fp); // XXX What about trying to discover the "header"? return $conf; } /** * Front to call getPointer and moving the resource to the * beginning of the file * Reset it if you like. * * @param string $file The name of the file * @param array &$conf The configuration * @param string $mode The open node (ex: FILE_MODE_READ or FILE_MODE_WRITE) * * @return boolean true on success false on failure */ function resetPointer($file, &$conf, $mode) { if (!File_CSV::getPointer($file, $conf, $mode, true)) { return false; } return true; }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -