How do I replace commas with dot in the quoted sub-string only? How do I replace commas with dot in the quoted sub-string only? codeigniter codeigniter

How do I replace commas with dot in the quoted sub-string only?


You can do that using preg_replace

$string = preg_replace('/("\d+),(\d+")/','$1.$2',$string);


Try this regular expression : https://regexr.com/3uvj0

$string = preg_replace('/(\".)(,)(.*\")/', '$1.$3', $string);


Please find work around for your requirement,

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

I took the reference of link. Here is your working code.