>假设我有一个字符串:
$string = 'apple, cat, dog, "0,445",symphony, "0,454"';
我想要的输出是:
$string = 'apple, cat, dog, "0.445",symphony, "0.454"';
您可以使用preg_replace
$string = preg_replace('/("d+),(d+")/','$1.$2',$string);
试试这个正则表达式: https://regexr.com/3uvj0
$string = preg_replace('/(".)(,)(.*")/', '$1.$3', $string);
请根据您的要求找到解决方法,
$string = 'apple, cat, dog, "0.445",symphony, "0,454"';
$array = str_replace('*comma*', ',', explode(',',preg_replace_callback('|"[^"]+"|', function ($matches) {return str_replace(',', '*comma*', $matches[0]);}, $string)));
foreach ($array as $key => $value) {
$array[$key] = str_replace(',', '.', $value);
}
$string = implode(",", $array);
我参考了链接。 这是您的工作代码。