使用preg_replace替换双引号之间的逗号 [英] replace comma between double quotes using preg_replace
本文介绍了使用preg_replace替换双引号之间的逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的csv文件
I have a csv file like this
Number,Name,UG College,UG University,PG College,PG University
1100225,Lakshmin,pkrrrr College,"PKRRRRR University, Choice",nandhaaaaa,"Nandhaa University, Kumali"
在读取文件时,我只想在双引号内替换逗号.
While reading the file I want to replace comma only inside double quotes .
谢谢.
推荐答案
$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;
这篇关于使用preg_replace替换双引号之间的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文