问题
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 .
Thanks in advance .
回答1:
$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;
来源:https://stackoverflow.com/questions/16481376/replace-comma-between-double-quotes-using-preg-replace