Skip to content
PHP Logo

How can PHP scripts be extended to automatically handle line breaks in text areas for output in different formats (text/plain or HTML)?

When outputting text from text areas in PHP scripts, line breaks may need to be handled differently depending on the output format (text/plain or HTML). To automatically handle line breaks for both formats, you can use the PHP `nl2br()` function to convert newline characters to HTML `<br>` tags when outputting text in HTML format. This will ensure that line breaks are displayed correctly in both text/plain and HTML outputs.

php
// Example PHP code snippet to handle line breaks in text areas for output in different formats

// Input text from a text area
$text = $_POST['textarea_input'];

// Output in text/plain format
echo nl2br($text);

// Output in HTML format
echo htmlentities($text);