Skip to content
PHP Logo

What are some potential solutions for displaying user-generated content with line breaks correctly in PHP textareas?

When displaying user-generated content with line breaks in PHP textareas, the issue often arises when the content is saved with newline characters (\n) but not displayed correctly on the frontend. One solution is to use the nl2br() function in PHP, which converts newline characters to HTML line breaks (<br>). This function ensures that line breaks are displayed properly when rendering the content on a webpage.

php
<?php
// Assuming $userContent contains the user-generated content with line breaks
$userContent = "This is a line with a\nline break.";

// Display the user-generated content with line breaks converted to HTML line breaks
echo nl2br($userContent);
?>