从字段中删除双引号

remove double quotes from field

这是我正在解析的csv文件的当前格式

"Street","City","Country"
"House # 3, Street "23, H, Block". Building 32", "CityName", "Country"

在这里你可以看到 23, H, Block 被双引号和逗号包围 - 当我使用下面的代码解析这个文件时

while (! feof($file)) {
    // provide last parameter so in case we get \ in a field it 
    // doesn't break the data
    $row = fgetcsv($file, null, ",", '"', '"');
    // so we don't send anything besides array
    if (count($row) > 0) {
        // if array is empty we don't pass it to further proceeding
        if ($row) {
            $sorted[] = array_merge($rows, $row);
        }
    }
}

解析将 23hBlock 分成不同的元素,而它们应该是一个

事情是这样的

array:2 [▼
  0 => array:3 [▼
    0 => "Street"
    1 => "City"
    2 => "Country"
  ]
  1 => array:5 [▼
    0 => "House # 3, Street 23"
    1 => " H"
    2 => " Block". Building 32""
    3 => "CityName"
    4 => "Country"
  ]
]

虽然我想要这样

array:2 [▼
  0 => array:3 [▼
    0 => "Street"
    1 => "City"
    2 => "Country"
  ]
  1 => array:3 [▼
    0 => "House # 3, Street 23, H, Block. Building 32"
    1 => "CityName"
    2 => "Country"
  ]
]

如果我可以使用一些正则表达式模式从整个 csv 文件中删除不需要的引号,那将会很有帮助

我认为您应该关注如何将 line/row 正确拆分为标记,而不是从行中删除不需要的双引号字符。

块分隔符的形式为 ","", " 因此分割行的正则表达式为

(?<="),\s*(?=")

参见 DEMO 和正则表达式解释