$a = 'aaa,bbb,ccc,"eee,ccc,sss,ppp",dddd,iiii,cccc';
如果碰到这种类型的字符串,我怎么才能使用正则来替换双引号里面的逗号呢?(注:双引号里面有几个逗号不是确定的,上面的只是列子)
------解决方案--------------------
$a = 'aaa,bbb,ccc,"eee,ccc,sss,ppp",dddd,iiii,cccc';aaa,bbb,ccc,"eee ccc sss ppp",dddd,iiii,cccc
echo preg_replace_callback('/".+?"/', function($r) { return str_replace(',', ' ', $r[0]);}, $a);
------解决方案--------------------
$a = 'aaa,bbb,ccc,"eee,ccc,sss,ppp",dddd,iiii,cccc';
$new=preg_replace('/"(.+?)"/ies',"replace('\\1')",$a);
echo $new;
//aaa,bbb,ccc,"eeecccsssppp",dddd,iiii,cccc
function replace($str){
$return=str_replace(',','',$str);
return '"'.$return.'"';
}
------解决方案--------------------
$a = 'aaa,bbb,ccc,"eee,ccc,sss,ppp",dddd,iiii,cccc';
echo preg_replace('/(?<=")(.+?)(?=")/e', 'str_replace(",", " ", "$1")',$a);