What are the best practices for saving data in CSV files to ensure compatibility with programs like Excel?
When saving data in CSV files to ensure compatibility with programs like Excel, it is important to properly format the data and handle special characters. To do this, you can enclose each field in double quotes and escape any double quotes within the data by doubling them. Additionally, use a comma as the delimiter and ensure that the file is saved with a .csv extension.
$data = [
['Name', 'Age', 'Email'],
['John Doe', 30, 'john.doe@example.com'],
['Jane Smith', 25, 'jane.smith@example.com'],
];
$fp = fopen('data.csv', 'w');
foreach ($data as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);