What is the function nl2br() used for in PHP and how can it help with formatting text input?
The nl2br() function in PHP is used to insert HTML line breaks (<br>) before all newlines in a string. This can help with formatting text input by converting newline characters into HTML line breaks, making the text display properly in an HTML document.
<?php
$text = "This is a text\nwith newlines";
$formatted_text = nl2br($text);
echo $formatted_text;
?>
Related Questions
- What potential pitfalls should be considered when using preg_replace in PHP to filter out non-numeric characters from a string?
- How can you differentiate between multiple forms on a webpage in PHP?
- What are the potential pitfalls of using timestamp and time data types in PHP when storing and querying date and time values in a database?