How can PHP developers ensure the flexibility and security of their code when handling user-generated content that may contain unexpected line breaks?

When handling user-generated content that may contain unexpected line breaks, PHP developers can ensure flexibility and security by sanitizing the input data to remove any potentially harmful characters and by using functions like nl2br() to properly handle line breaks in the output. Additionally, developers should validate and filter the input data to prevent any malicious code injection.

// Sanitize user input to remove potentially harmful characters
$clean_input = filter_var($_POST['user_input'], FILTER_SANITIZE_STRING);

// Handle line breaks properly using nl2br() function
echo nl2br($clean_input);