Php CSV导入按逗号拆分-如何处理引号?

Php CSV导入按逗号拆分-如何处理引号?,php,regex,csv,Php,Regex,Csv,我正在导入一个CSV文件,但遇到了一个问题。数据格式如下: 测试690,“这是测试1、2和3”,14.95美元,4美元 我需要能够按展开,不在引号内…请参见函数 如果已经有一个字符串,可以创建一个对其进行包装的流,然后使用fgetcsv。请参见如果您真的想手工完成此操作,下面是我编写的一个粗略参考实现,用于将完整的CSV文本行分解为一个数组警告:此代码不处理多行字段!在这个实现中,整个CSV行必须存在于一行中,没有换行符 <?php //--------------------------

我正在导入一个CSV文件,但遇到了一个问题。数据格式如下:

测试690,“这是测试1、2和3”,14.95美元,4美元

我需要能够按展开,不在引号内…

请参见函数


如果已经有一个字符串,可以创建一个对其进行包装的流,然后使用
fgetcsv
。请参见

如果您真的想手工完成此操作,下面是我编写的一个粗略参考实现,用于将完整的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));
}
?>

我想尝试的一件事是,如果可以的话,更改输入文件,使所有内容都用引号括起来,然后在去掉第一个和最后一个引号后,您可以按
“,”
进行分解。这样,它就不会在引号旁边爆炸逗号。当然,只有当你不想像Artefactor建议的那样使用
fgetcsv
并且你想用它来挑战自己时,才可以这样做。我不能用引号将所有内容包装起来,它是通过另一个系统导出的。引号是否只能在第二个字段上使用?根据普遍接受的CSV规范,引号是可选的,并且只需要消除包含引号、逗号或多行的字段的歧义。我宁愿使用正则表达式,因为它有特殊的功能,而不是使用正则表达式。它不像看上去那么简单。字符串中可能有换行符。您可能有转义字符。一旦CSV被解析(通过fgetscsv),您就可以对每个字段进行正则表达式处理,使其符合您的心意。需要注意的是,
fgetcsv
有吃特殊字符的问题,如果它们是字符串值的第一个字母,那么您有时只需处理它。请参阅: