How can the functions file(), str_replace(), and fwrite() be effectively used to replace semicolons with commas in a CSV file in PHP?

To replace semicolons with commas in a CSV file using PHP, you can read the file using the file() function, replace the semicolons with commas using str_replace(), and then write the modified content back to the file using fwrite().

$file = 'example.csv';
$data = file($file);

foreach ($data as &$line) {
    $line = str_replace(';', ',', $line);
}

$fp = fopen($file, 'w');
foreach ($data as $line) {
    fwrite($fp, $line);
}
fclose($fp);