What function in PHP can be used to convert newline characters to HTML line breaks for displaying text from a database?
When displaying text from a database in HTML, newline characters (\n) need to be converted to HTML line breaks (<br>) in order to properly format the text with line breaks. This can be achieved using the PHP function nl2br(), which converts newline characters to <br> tags.
$text_from_database = "This is a text with\nnewline characters.";
$formatted_text = nl2br($text_from_database);
echo $formatted_text;
Related Questions
- How can PHP developers ensure that regular expressions work consistently across different platforms, especially when dealing with line breaks and special characters like \n and \r?
- What potential issue is the user facing with the PHP code provided for displaying the latest post in a forum board?
- What is the potential issue with encoding when sending emails using PHPMailer?