Is it advisable to create a custom HTML filter for protection against XSS in PHP, or is it better to use pre-built solutions like HTML Purifier?

When dealing with protection against XSS attacks in PHP, it is generally recommended to use pre-built solutions like HTML Purifier rather than creating a custom HTML filter. Pre-built solutions have been thoroughly tested and are more likely to cover a wide range of XSS vulnerabilities, providing a more robust defense mechanism.

// Example of using HTML Purifier to sanitize input
require_once 'path/to/HTMLPurifier.auto.php';

$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);

$dirty_html = "<script>alert('XSS attack!')</script>";
$clean_html = $purifier->purify($dirty_html);

echo $clean_html;