Php CSV使用regexp删除引号中的逗号
我有一个CSV文件,我们知道excel在字段中使用逗号,用双引号括起来,例如我有一个文件Php CSV使用regexp删除引号中的逗号,php,regex,csv,Php,Regex,Csv,我有一个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 作为输出这将进行多次替换,并删除引号 <?php $data = 'Product Name,Product Code P
Product Name,Product Code
Product 1,AAA
"Prod,A,B",BBB
如何使用RegExp将引号替换为“.”,但只能在引号内,以便
Product Name,Product Code
Product 1,AAA
Prod.A.B,BBB
作为输出这将进行多次替换,并删除引号
<?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;
?>
这将执行多次替换,并删除引号
<?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;
?>
CSV处理函数(,)在这方面要好得多-它们将处理边缘情况,并且可能比您能想到的任何正则表达式都要可靠得多
// 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);
CSV处理函数(,)在这方面要好得多——它们将处理边缘情况,并且可能比您能想到的任何正则表达式都要可靠得多
// 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);
你为什么要这么做?PHP的CSV处理函数能够处理可选的引号括起的字段。了解此->我直接从转换为文本块字段的数据库中读取CSV,我不想将其写入离散字段。结果表明PHPs CSV函数很好,但请确保正确编码文件,无法正确使用UTF8编码的报价单为什么需要这样做?PHP的CSV处理函数能够处理可选的引号括起的字段。了解此->我直接从转换为文本块字段的数据库中读取CSV,我不想将其写入离散字段。结果表明PHPs CSV函数很好,但请确保正确编码文件,不能正确使用UTF8编码的报价,非常好!对于其他人,如果此页面上没有任何内容,请确保您的文件具有正确的编码works@Akshat非常有效-直到值还包含双引号。然后它中断,因为正则表达式不考虑逃逸。这就是为什么CSV处理函数是实现这一点的方法——你所能做的任何事情都不会像它们那样处理边缘情况。效果非常好!对于其他人,如果此页面上没有任何内容,请确保您的文件具有正确的编码works@Akshat非常有效-直到值还包含双引号。然后它中断,因为正则表达式不考虑逃逸。这就是为什么CSV处理函数是实现这一点的方法——您所能做的任何事情都不会像它们那样处理边缘情况。