Skip to content

Beautiful Open-Source Form Builder

feat: enhance CSV export functionality with row sanitization

Chirag Chhatralaa4cbaced76a2
diff --git a/api/app/Service/Forms/FormExportService.php b/api/app/Service/Forms/FormExportService.php
index a5b56335e..a2d2ec2e5 100644
--- a/api/app/Service/Forms/FormExportService.php
+++ b/api/app/Service/Forms/FormExportService.php
@@ -100,11 +100,12 @@ class FormExportService
// Write header row (clean column names)
$headers = $this->cleanColumnNames(array_keys($rows[0]));
- fputcsv($output, $headers);
+ fputcsv($output, $headers, ',', '"', '');
- // Write data rows
+ // Write data rows (sanitize values to prevent newlines breaking Excel/parsers)
foreach ($rows as $row) {
- fputcsv($output, array_values($row));
+ $sanitizedRow = $this->sanitizeRowForCsv($row);
+ fputcsv($output, array_values($sanitizedRow), ',', '"', '');
}
rewind($output);
@@ -114,6 +115,22 @@ class FormExportService
return $csvContent;
}
+ /**
+ * Sanitize row values for CSV export.
+ * Replaces newlines with spaces to prevent multi-line fields that break
+ * Excel, Google Sheets, and other parsers that don't handle RFC 4180 multi-line fields.
+ */
+ private function sanitizeRowForCsv(array $row): array
+ {
+ return collect($row)->map(function ($value) {
+ if (! is_string($value)) {
+ return $value;
+ }
+
+ return preg_replace('/\r\n|\r|\n/', ' ', $value);
+ })->toArray();
+ }
+
/**
* Clean column names by removing parenthetical content
*/