我有一个CSV文件,我们知道excel通过用双引号将它们括在一个字段中用逗号来做它的事情,例如我有一个文件
Product Name,Product Code
Product 1,AAA
"Prod,A,B",BBB
如何使用RegExp将引号替换为“。”相反,但只在引号内,所以我得到
Product Name,Product Code
Product 1,AAA
Prod.A.B,BBB
作为输出
答案 0 :(得分: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);
编辑关于您不想读取/写入磁盘的评论,您可以这样做:
// 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);
答案 1 :(得分:2)
这将进行多次替换,并删除引号。
<?php
$data = 'Product Name,Product Code
Product 1,AAA
"Prod,A,B",BBB';
$rgx = '/"(.+?)"/';
preg_match_all($rgx, $data, $matches);
$x = 0; $max = count($matches[0]);
while($x < $max){
$replace = str_replace(",", ".", $matches[1][$x]);
$data = str_replace($matches[0][$x], $replace, $data);
$x++;
}
echo $data;
?>