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

时间:2013-05-10 11:38:17

标签: php explode

我有一个像这样的csv文件

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

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

提前致谢。

1 个答案:

答案 0 :(得分:2)

$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;