PHP preg_替换

PHP preg_替换,php,preg-replace,Php,Preg Replace,*强文本*我有一个像“x”,“x,y”,“x,y,h”这样的字符串 我想使用preg replace删除双引号内的逗号,并将字符串返回为 “x”、“xy”、“xyh”您只需使用常规替换即可 $mystring = str_replace(",", "", $mystring); 你可以用普通的替换品 $mystring = str_replace(",", "", $mystring); 您不需要preg\u replace() $string = str_replace(',', '',

*强文本*我有一个像“x”,“x,y”,“x,y,h”这样的字符串 我想使用preg replace删除双引号内的逗号,并将字符串返回为


“x”、“xy”、“xyh”

您只需使用常规替换即可

$mystring = str_replace(",", "", $mystring);

你可以用普通的替换品

$mystring = str_replace(",", "", $mystring);

您不需要
preg\u replace()

$string = str_replace(',', '', $string);

您不需要
preg\u replace()

$string = str_replace(',', '', $string);

我使用下面的方法,我发现对于这种类型的替换,它通常比regexp快

$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);

我使用下面的方法,我发现对于这种类型的替换,它通常比regexp快

$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”
@Michiel-在这种情况下,更改$I=false的初始化;至$i=真;我得到了相反的结果:
“x”“x,y”“x,y,h”
@Michiel-在这种情况下,更改$I=false的初始化;至$i=真;