Skip to content
PHP Logo

What is the function in PHP that can be used to strip HTML tags from a string?

When working with user-generated content, it is often necessary to strip HTML tags from a string to prevent potential security vulnerabilities such as cross-site scripting (XSS) attacks. One way to achieve this in PHP is by using the `strip_tags()` function. This function removes all HTML and PHP tags from a given string, leaving only the plain text content.

php
// Example of using strip_tags() function to remove HTML tags from a string
$htmlString = "<p>This is a <strong>sample</strong> text with <a href='#'>HTML</a> tags.</p>";
$plainText = strip_tags($htmlString);
echo $plainText;