Skip to content
PHP Logo

What functions can be used to convert newlines to HTML line breaks and strip HTML tags in PHP?

To convert newlines to HTML line breaks, you can use the nl2br() function in PHP. To strip HTML tags from a string, you can use the strip_tags() function. These functions are useful for formatting text input from users or cleaning up HTML content.

php
// Convert newlines to HTML line breaks
$text_with_newlines = "Hello\nWorld";
$text_with_line_breaks = nl2br($text_with_newlines);

echo $text_with_line_breaks; // Output: Hello<br />World

// Strip HTML tags
$html_content = "<p>This is <strong>bold</strong> text</p>";
$clean_text = strip_tags($html_content);

echo $clean_text; // Output: This is bold text