php - 使用 preg_replace 替换双引号之间的逗号

标签 php explode

我有一个像这样的 csv 文件

Number,Name,UG College,UG University,PG College,PG University
1100225,Lakshmin,pkrrrr College,"PKRRRRR University, Choice",nandhaaaaa,"Nandhaa University, Kumali"

在阅读文件时,我只想替换双引号内的逗号。

提前致谢。

最佳答案

$string = '1100225,Lakshmin,pkrrrr College,"PKRRRRR University, Choice",nandhaaaaa,"Nandhaa University, Kumali"';

$string = preg_replace_callback(
        '|"[^"]+"|',
        create_function(
            // single quotes are essential here,
            // or alternative escape all $ as \$
            '$matches',
            'return str_replace(\',\',\'*comma*\',$matches[0]);'
        ),$string );
$array = explode(',',$string);
$array = str_replace('*comma*',',',$array);
print_r($array);       
exit;

关于php - 使用 preg_replace 替换双引号之间的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16481376/

相关文章:

php - 使用 CodeIgniter 通过 where 子句连接两个表

php - 循环直到变量达到 0

php - 如何在Bootstrap中将双按钮设置为 "fire"

php - 在同一行中使用 explode() 时如何访问数组索引?

php - 序列化或内爆

php - PHP 的explode() 函数在C++ 中是否有等价物?

javascript - 提交后ajax表单重定向

php - 为什么这段 PHP 代码在 Linux 中带双引号的命令行上不起作用

php - 不在 () 之间时分解字符串

php - explode() 给了我我不期望的空白值