What are common pitfalls when storing data in CSV files using PHP, especially when dealing with line breaks and paragraphs?

When storing data in CSV files using PHP, a common pitfall is dealing with line breaks and paragraphs. To solve this issue, you can enclose your data in double quotes to ensure that line breaks are preserved within a cell. Additionally, you may need to escape any double quotes within the data by doubling them up.

// Data to be stored in CSV file
$data = "This is a paragraph with line breaks.\nIt spans multiple lines.";

// Escape double quotes and enclose data in double quotes
$data = '"' . str_replace('"', '""', $data) . '"';

// Write data to CSV file
$file = fopen('data.csv', 'w');
fputcsv($file, [$data]);
fclose($file);