In the context of PHP development, what best practices should be followed when dealing with user input containing line breaks and ensuring proper formatting when saving data to a file?

When dealing with user input containing line breaks in PHP, it is important to sanitize the input to prevent any potential security vulnerabilities. To ensure proper formatting when saving data to a file, you can use the PHP function `nl2br()` to convert line breaks to HTML line breaks before saving the data.

// Sanitize user input containing line breaks
$userInput = filter_var($_POST['user_input'], FILTER_SANITIZE_STRING);

// Convert line breaks to HTML line breaks
$formattedInput = nl2br($userInput);

// Save formatted data to a file
$file = fopen("data.txt", "w");
fwrite($file, $formattedInput);
fclose($file);