How can one ensure that user input in a CSV file does not contain dangerous characters like tab or carriage return?

To ensure that user input in a CSV file does not contain dangerous characters like tab or carriage return, you can sanitize the input by removing these characters before writing it to the file. One way to do this is by using PHP's `str_replace` function to replace these characters with an empty string.

// Sanitize user input before writing to CSV file
$userInput = "User input with dangerous characters \t \r";
$sanitizedInput = str_replace(["\t", "\r"], "", $userInput);

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