CSV Import Split by Comma - что делать с цитатами? - PullRequest
4 голосов
/ 14 мая 2010

У меня есть файл CSV, который я импортирую, но столкнулся с проблемой. Данные в формате:

TEST 690, "This is a test 1, 2 and 3" ,$14.95 ,4

Мне нужно уметь взорваться тем, что не в кавычках ...

Ответы [ 2 ]

9 голосов
/ 14 мая 2010

См. Функцию fgetcsv .

Если у вас уже есть строка, вы можете создать поток, который обернет ее, а затем использовать fgetcsv. Смотри http://code.google.com/p/phpstringstream/source/browse/trunk/stringstream.php

5 голосов
/ 14 мая 2010

Если вы действительно хотите сделать это вручную, вот грубая справочная реализация, которую я написал, чтобы разбить полную строку текста CSV в массив. Предупреждаем : этот код НЕ обрабатывает многострочные поля! В этой реализации вся строка CSV должна существовать на одной строке без разрывов строк!

<?php
//-----------------------------------------------------------------------
function csvexplode($str, $delim = ',', $qual = "\"")
// Explode a single CSV string (line) into an array.
{
    $len = strlen($str);  // Store the complete length of the string for easy reference.
    $inside = false;  // Maintain state when we're inside quoted elements.
    $lastWasDelim = false;  // Maintain state if we just started a new element.
    $word = '';  // Accumulator for current element.

    for($i = 0; $i < $len; ++$i)
    {
        // We're outside a quoted element, and the current char is a field delimiter.
        if(!$inside && $str[$i]==$delim)
        {
            $out[] = $word;
            $word = '';
            $lastWasDelim = true;
        } 

        // We're inside a quoted element, the current char is a qualifier, and the next char is a qualifier.
        elseif($inside && $str[$i]==$qual && ($i<$len && $str[$i+1]==$qual))
        {
            $word .= $qual;  // Add one qual into the element,
            ++$i; // Then skip ahead to the next non-qual char.
        }

        // The current char is a qualifier (so we're either entering or leaving a quoted element.)
        elseif ($str[$i] == $qual)
        {
            $inside = !$inside;
        }

        // We're outside a quoted element, the current char is whitespace and the 'last' char was a delimiter.
        elseif( !$inside && ($str[$i]==" ")  && $lastWasDelim)
        {
            // Just skip the char because it's leading whitespace in front of an element.
        }

        // Outside a quoted element, the current char is whitespace, the "next" char is a delimiter.
        elseif(!$inside && ($str[$i]==" ")  )
        {
            // Look ahead for the next non-whitespace char.
            $lookAhead = $i+1;
            while(($lookAhead < $len) && ($str[$lookAhead] == " ")) 
            {
                ++$lookAhead;
            }

            // If the next char is formatting, we're dealing with trailing whitespace.
            if($str[$lookAhead] == $delim || $str[$lookAhead] == $qual) 
            {
                $i = $lookAhead-1;  // Jump the pointer ahead to right before the delimiter or qualifier.
            }

            // Otherwise we're still in the middle of an element, so add the whitespace to the output.
            else
            {
                $word .= $str[$i];  
            }
        }

        // If all else fails, add the character to the current element.
        else
        {
            $word .= $str[$i];
            $lastWasDelim = false;
        }
    }

    $out[] = $word;
    return $out;
}


// Examples:

$csvInput = 'Name,Address,Phone
Alice,123 First Street,"555-555-5555"
Bob,"345 Second Place,   City  ST",666-666-6666
"Charlie ""Chuck"" Doe",   3rd Circle   ,"  777-777-7777"';

// explode() emulates file() in this context.
foreach(explode("\n", $csvInput) as $line)
{
    var_dump(csvexplode($line));
}
?>

Я все же рекомендовал бы полагаться на встроенную функцию PHP. Это (надеюсь) будет гораздо надежнее в долгосрочной перспективе. Артефакто и Roadmaster правы: все, что вам нужно сделать с данными, лучше всего делать после того, как вы их импортируете.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...