What is the best practice for handling line breaks in text input for a messaging system in PHP?

When handling line breaks in text input for a messaging system in PHP, it is important to properly sanitize and format the input to ensure consistency and readability for users. One common approach is to replace any line breaks with HTML line break tags (<br>) before displaying the text on the front end. This allows for the text to be displayed with proper line breaks while preventing any potential security vulnerabilities.

// Sanitize input and replace line breaks with HTML line break tags
$inputText = $_POST[&#039;message&#039;];
$sanitizedText = nl2br(htmlspecialchars($inputText));

// Display the sanitized text with line breaks
echo $sanitizedText;