* strong text *我有一个像这样的字符串“x”,“x,y”,“x,y,h” 我想用户preg替换删除逗号内的双重qutations和返回的字符串为PHP preg_replace
“X”, “XY”, “XYH”
* strong text *我有一个像这样的字符串“x”,“x,y”,“x,y,h” 我想用户preg替换删除逗号内的双重qutations和返回的字符串为PHP preg_replace
“X”, “XY”, “XYH”
你可以只使用常规的更换。
$mystring = str_replace(",", "", $mystring);
你不需要preg_replace()
这里徘徊无论可能,你应该尽量避免它
$string = str_replace(',', '', $string);
我用下面,我已经找到了比通常更快的正则表达式是这种类型的更换
的$string = '"x", "x,y" , "x,y,h"';
$temp = explode('"',$string);
$i = true;
foreach($temp as &$value) {
// Only replace in alternating array entries, because these are the entries inside the quotes
if ($i = !$i) {
$value = str_replace(',', '', $value);
}
}
unset($value);
// Then rebuild the original string
$string = implode('"',$temp);
我得到了相反的结果:'“x”“x,y”“x,y,h”'。 – 2011-03-15 09:26:15
@Michiel - 在这种情况下,更改$ i = false的初始化;到$ i = true; – 2011-03-15 09:28:00
@Michiel - 谢谢指出逆转 – 2011-03-15 09:30:53
这将很好地工作:http://codepad.org/lq7I5wkd
<?php
$myStr = '"x", "x,y" , "x,y,h"';
$chunks = preg_split("/\"[\s]*[,][\s]*\"/", $myStr);
for($i=0;$i<count($chunks);$i++)
$chunks[$i] = str_replace(",","",$chunks[$i]);
echo implode('","',$chunks);
?>
他不想删除所有逗号。请阅读这个问题。 – 2011-03-15 09:14:14