What are some best practices for removing HTML tags from a string in PHP?
When working with user-generated content or data from external sources, it's important to sanitize input to prevent XSS attacks and ensure data integrity. One common task is removing HTML tags from a string in PHP to display plain text content. One way to achieve this is by using the strip_tags() function in PHP, which removes all HTML and PHP tags from a string.
// Input string with HTML tags
$inputString = "<p>This is a <strong>sample</strong> text with <a href='#'>HTML</a> tags.</p>";
// Remove HTML tags from the input string
$cleanString = strip_tags($inputString);
// Output the cleaned string
echo $cleanString;