What are potential pitfalls when using strip_tags() and nl2br() functions in PHP to format messages for users?

When using strip_tags() and nl2br() functions in PHP to format messages for users, a potential pitfall is that strip_tags() may remove necessary HTML tags, leading to unexpected formatting issues. To solve this, it is recommended to apply strip_tags() after nl2br() to preserve line breaks while removing any potentially harmful HTML tags.

$message = "<p>Hello, <strong>user</strong>! This is a message with <a href='#'>HTML</a> tags.</p>";
$formatted_message = nl2br($message);
$clean_message = strip_tags($formatted_message);
echo $clean_message;