Horde_Data_Csv::importFile PHP Method

importFile() public method

Imports and parses a CSV file.
public importFile ( string $filename, boolean $header = false, string $sep = ',', string $quote = '', integer $fields = null, $import_mapping = [], string $charset = null, string $crlf = null ) : array
$filename string The name of the file to parse.
$header boolean Does the first line contain the field/column names?
$sep string The field/column separator.
$quote string The quoting character.
$fields integer The number or fields/columns.
$charset string The file's charset.
$crlf string The file's linefeed characters.
return array A two-dimensional array of all imported data rows. If $header was true the rows are associative arrays with the field/column names as the keys.
    public function importFile($filename, $header = false, $sep = ',', $quote = '', $fields = null, $import_mapping = array(), $charset = null, $crlf = null)
    {
        if (empty($fields)) {
            return array();
        }
        $conf = array('length' => $fields, 'quote' => $quote, 'separator' => $sep);
        $fp = @fopen($filename, 'r');
        if (!$fp) {
            throw new Horde_Data_Exception(Horde_Data_Translation::t("Cannot open file."));
        }
        /* Strip and keep the first line if it contains the field names. */
        if ($header) {
            $head = self::getCsv($fp, $conf);
            if (!$head) {
                return array();
            }
            if (!empty($charset)) {
                $head = Horde_String::convertCharset($head, $charset, 'UTF-8');
            }
        }
        $data = array();
        while ($line = self::getCsv($fp, $conf)) {
            if (!empty($charset)) {
                $line = Horde_String::convertCharset($line, $charset, 'UTF-8');
            }
            if (!isset($head)) {
                $data[] = $line;
            } else {
                $newline = array();
                for ($i = 0; $i < count($head); $i++) {
                    if (isset($import_mapping[$head[$i]])) {
                        $head[$i] = $import_mapping[$head[$i]];
                    }
                    $cell = $line[$i];
                    $cell = preg_replace("/\"\"/", "\"", $cell);
                    $newline[$head[$i]] = empty($cell) ? '' : $cell;
                }
                $data[] = $newline;
            }
        }
        return $data;
    }

Usage Example

Esempio n. 1
0
    public function testImportFile()
    {
        $data = new Horde_Data_Csv(new Horde_Data_Storage_Mock());
        $expected = array(array(0 => 'one', 1 => 'two', 2 => 'three four', 3 => 'five'), array(0 => 'six', 1 => 'seven', 2 => 'eight nine', 3 => 'ten'));
        $this->assertEquals($expected, $data->importFile(__DIR__ . '/fixtures/simple_dos.csv', false, ',', '', 4));
        $this->assertEquals($expected, $data->importFile(__DIR__ . '/fixtures/simple_unix.csv', false, ',', '', 4));
        $expected = array(array('one' => 'six', 'two' => 'seven', 'three four' => 'eight nine', 'five' => 'ten'));
        $this->assertEquals($expected, $data->importFile(__DIR__ . '/fixtures/simple_dos.csv', true, ',', '', 4));
        $this->assertEquals($expected, $data->importFile(__DIR__ . '/fixtures/simple_unix.csv', true, ',', '', 4));
        $expected = array(array('one' => 'four', 'two' => 'five"six', 'three' => 'seven
"eight"', '' => ''));
        $this->assertEquals($expected, $data->importFile(__DIR__ . '/fixtures/quotes1.csv', true, ',', '"', 4));
        $this->assertEquals($expected, $data->importFile(__DIR__ . '/fixtures/quotes2.csv', true, ',', '"', 4));
    }