Skip to content

Instantly share code, notes, and snippets.

@zecka
Created May 27, 2020 12:51
Show Gist options
  • Save zecka/911ee29f30d7b2f85b84f9dc2b13c377 to your computer and use it in GitHub Desktop.
Save zecka/911ee29f30d7b2f85b84f9dc2b13c377 to your computer and use it in GitHub Desktop.
Example use of HTMLPurifier PHP
<?php
// composer install ezyang/htmlpurifier
require 'vendor/autoload.php';
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.AllowedElements', 'a,i,b,p,div,strong,h1,h2,h3,h4,h5,ol,ul,li,br');
$config->set('HTML.AllowedAttributes', 'href,title,target,name');
$config->set('HTML.Attr.Name.UseCDATA', true);
$config->set('AutoFormat.RemoveEmpty.RemoveNbsp', true);
$config->set('Attr.AllowedFrameTargets', ['_blank']);
$config->set('AutoFormat.RemoveEmpty', true);
$purifier = new HTMLPurifier($config);
$clean_html = $purifier->purify($content);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment