php怎么去除多余的逗号 例如:一串字符串 111,222,,,333,444,,处理之后返回 111,222,333,44

字符串的逗号是无规则如:111,222,,,333,444,,又如:,,,111,222,,,333,444又如:111,222,,,333,444又如:,,,1881,,,,414,,,,,又如:,,,111,222,,,333,又如:,,,111,其中数也不规定长度的

$str="111,222,,,333,,444,,,,666";
$sttr=str_replace(',,,',','$str);
$sttr=str_replace(',,,,',','$str);追问

字符串的逗号是无规则

追答

$str="111,222,,,333,,444,,,,666";
$arr_str=explode(',',$str);
$arr_str=array_filter($arr_str);

foreach($arr_str as $val){
$ret[]=$val;

}
$new_str=implode(','$ret);
print_r($new_str);

温馨提示:内容为网友见解,仅供参考
第1个回答  2013-04-06
8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

...111,222,,,333,444,,处理之后返回 111,222,333,44
str="111,222,,,333,,444,,,666";sttr=str_replace(',,,',','$str);sttr=str_replace(',,,',','$str);

...111,222,,,333,444,, 处理之后返回 111,222,333,444
用正则提取数字不就行了?var str="111,222,,,333,444,, ";str=str.match(\/\\d+\/g).join(",");alert(str);

相似回答