how to write a regex to remove extra commas from string for making a csv:-
Explanation:- Basically I am getting this kind of data in a string:- $string = 'abc,bcd,cdf, "af,cv"'
this is a sample string and I want to make csv from that string. Now I want to remove that extra comma between af and cv but not the other commas. Can someone help in writing the regex.
i think this will resolve your issue:-
$result_1 = "STRING containing extra commas"
$regex = '/"(.+?)"/';
preg_match_all($regex, $result_1, $matches);
$x = 0; $max = count($matches[0]);
while($x < $max){
$replace = str_replace(",", ".", $matches[1][$x]);
$result_1 = str_replace($matches[0][$x], $replace, $result_1);
$x++;
}
print_r($result_1);