What are the best practices for removing specific HTML tags while preserving others in PHP?
When removing specific HTML tags while preserving others in PHP, one approach is to use the strip_tags() function with the exception parameter to specify the tags that should not be removed. This allows you to selectively remove certain tags while keeping others intact.
// HTML content with specific tags to preserve and remove
$html = '<div><p>Hello</p><a href="#">Link</a><script>alert("Hello")</script></div>';
// Remove all tags except <p> and <a>
$cleaned_html = strip_tags($html, '<p><a>');
echo $cleaned_html;
Keywords
Related Questions
- What steps can be taken to troubleshoot and fix errors related to file path specifications in PHP form submissions?
- What are some potential pitfalls when converting Perl scripts to PHP, as seen in the provided code examples?
- Are there any best practices for displaying content from a database in PHP to avoid losing data or formatting issues?