Skip to content
PHP Logo

How can PHP developers handle line breaks and special characters when processing text from a form in PHP?

When processing text from a form in PHP, developers can use functions like nl2br() to handle line breaks and htmlspecialchars() to handle special characters. These functions help sanitize user input and prevent potential security vulnerabilities like cross-site scripting attacks.

php
// Handling line breaks and special characters in PHP form processing
$text = $_POST['text']; // Assuming 'text' is the name of the form field
$clean_text = htmlspecialchars(nl2br($text));
echo $clean_text;