CSV使用regexp删除引号中的逗号

Aks*_*hat 1 php regex csv

我有一个CSV文件,我们知道excel通过用双引号将它们括在一个字段中用逗号来做它的事情,例如我有一个文件

Product Name,Product Code
Product 1,AAA
"Prod,A,B",BBB
Run Code Online (Sandbox Code Playgroud)

如何使用RegExp将引号替换为"." 相反,但只在引号内,所以我得到

Product Name,Product Code
Product 1,AAA
Prod.A.B,BBB
Run Code Online (Sandbox Code Playgroud)

作为输出

Dav*_*dom 5

CSV处理函数(fgetcsv(),fputcsv())对此更好 - 它们将处理边缘情况,并且可能比您可以提出的任何正则表达式更可靠.

// Open the file
$fp = fopen($pathToCsvFile, 'r+');

// Create an array of modified data
$tmp = array();
while (($row = fgetcsv($fp, 8192)) !== FALSE) {
  foreach ($row as &$field) $field = str_replace(',', '.', $field);
  $tmp[] = $row;
}

// Truncate the file and put the pointer at the beginning
ftruncate($fp, 0);
rewind($fp);

// Write the modified data back and close the file
foreach ($tmp as $row) {
  fputcsv($fp, $row);
}
fclose($fp);
Run Code Online (Sandbox Code Playgroud)

编辑关于您不想读/写磁盘的评论后,您可以这样做:

// Lets say the raw CSV data is held in this variable as a string
$rawCsvData = 'Product Name,Product Code
Product 1,AAA
"Prod,A,B",BBB';

// Open a virtual file pointer to memory and fill it with your data
$fp = fopen('php://memory', 'w+');
fwrite($fp, $rawCsvData);

// Start from the beginning of the pointer
rewind($fp);

// ... INSERT CODE FROM ABOVE HERE (minus the fopen()/fclose())

$modifiedCsvData = stream_get_contents($fp);
fclose($fp);
Run Code Online (Sandbox Code Playgroud)