- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
/*
	Функция для парсинга CSV файла. 
	Автор: Федорченко Антон Александрович ([email protected], http://sites.neeweb.com/antfsite)
	
	Возвращает двумерный массив:
		array(
			array ( ... )	//Строка 1
			array ( ... )	//Строка 2
			...
			array ( ... )	//Строка n
			)
*/
function parse_csv($filename, $codepage = 'windows-1251') {
    $csv_lines  = file($filename);
    $skip_char = false;
    $column = '';
    if (is_array($csv_lines)) {
        $cnt = count($csv_lines);
        for($i = 0; $i < $cnt; $i++) {
            $line = trim($csv_lines[$i]);
            $first_char = true;
            $col_num = 0;
            $length = strlen($line);
            for ($b = 0; $b < $length; $b ++) {
                if ($skip_char != true) {
                    $process = true;
                    if ($first_char == true) {
                        if($line[$b] == '"') {
                            $terminator = '";';
                            $process = false;
                        } else {
                            $terminator = ';';
                        }
                        $first_char = false;
                    }
                    if ($line[$b] == '"') {
                        $next_char = $line[$b + 1];
                        if ($next_char == '"') {
                            $skip_char = true;
                        } elseif ($next_char == ';') {
                            if($terminator == '";') {
                                $first_char = true;
                                $process = false;
                                $skip_char = true;
                            }
                        }
                    }
                    if ($process == true) {
                        if ($line[$b] == ';') {
                            if ($terminator == ';') {
                                $first_char = true;
                                $process = false;
                            }
                        }
                    }
                    if ($process == true) $column .= $line[$b];
                    if ($b == ($length - 1)) $first_char = true;
                    if ($first_char == true) {
                        $values[$i][$col_num] = $column;
                        $column = '';
                        $col_num ++;
                    }
                } else {
                    $skip_char = false;
                }
            }
        }
    }
    if (strtolower($codepage) != "utf-8") {
        foreach ($values as $lineIndex => $line) {
            foreach ($line as $cellIndex => $cell) $values[$lineIndex][$cellIndex] = iconv($codepage, "UTF-8", $cell);
        }
    }
    return $values;
}
                                 
        
            Жаль, не пришло в голову запостить сразу - ОНО ещё и неотворматировано было.
Антон Александрович - мощный дядька =)
        
        
Вокруг люди сидят, трудятся - ржать немного неудобно. Но приходится...
if ($first_char == true) {
Не ну крассиво. Индуссы рукоплещут.
указаная выше fgetcsv() - тыкайте здесь https://bugs.php.net/bug.php?id=50686 - сама похоже говно.