protected function decode($line) { if (empty($line)) { return array(); } $data = api_str_getcsv($line, $this->get_delimiter(), $this->get_enclosure()); if ($this->headers) { $result = array(); foreach ($data as $index => $value) { $key = isset($this->headers[$index]) ? $this->headers[$index] : false; if ($key) { $result[$key] = $value; } else { $result[] = $value; } } } else { $result = $data; } return $result; }
/** * Reads a line from a file pointer and parses it for CSV fields. This function is not affected by the OS-locale settings. * @param resource $handle The file pointer, it must be valid and must point to a file successfully opened by fopen(). * @param int $length (optional) Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). * If no length is specified, it will keep reading from the stream until it reaches the end of the line. * @param string $delimiter (optional) The field delimiter, one character only. The default delimiter character is comma {,). * @param string $enclosure (optional) The field enclosure, one character only. The default enclosure character is quote ("). * @param string $escape (optional) The escape character, one character only. The default escape character is backslash (\). * @return array Returns an array containing the fields read. * Note: In order this function to work correctly with UTF-8, limitation for the parameters $delimiter, $enclosure and $escape * should be kept. These parameters should be single ASCII characters only. * @link http://php.net/manual/en/function.fgetcsv.php */ function api_fgetcsv($handle, $length = null, $delimiter = ',', $enclosure = '"', $escape = '\\') { if (($line = is_null($length) ? fgets($handle): fgets($handle, $length)) !== false) { $line = rtrim($line, "\r\n"); return api_str_getcsv($line, $delimiter, $enclosure, $escape); } return false; }