Home  >  Article  >  Backend Development  >  一个关于正则去除小数点的有关问题,请大侠指教

一个关于正则去除小数点的有关问题,请大侠指教

WBOY
WBOYOriginal
2016-06-13 12:02:581168browse

一个关于正则去除小数点的问题,请大侠指教

<br />$a = 'aaa,bbb,ccc,"eee,ccc,sss,ppp",dddd,iiii,cccc';<br />

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

------解决方案--------------------
<br />$a = 'aaa,bbb,ccc,"eee,ccc,sss,ppp",dddd,iiii,cccc';<br />
$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';<br />echo preg_replace('/(?<=")(.+?)(?=")/e', 'str_replace(",", " ", "$1")',$a);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn