Reputation: 26281
Is it possible to take the below original code, and wrap it in a new class so I may simply create $filter = new myPurifier();
and then use $output = $filter->purify($input);
? At the bottom of this post, you will see my failed attempt to do so. Thanks
original code:
include_once('htmlpurifier/library/HTMLPurifier.auto.php');
$config = HTMLPurifier_Config::createDefault();
$config->set('Core', 'Encoding', 'UTF-8');
$config->set('HTML', 'Doctype', 'HTML 4.01 Transitional');
if (defined('PURIFIER_CACHE')) {
$config->set('Cache', 'SerializerPath', PURIFIER_CACHE);
} else {
# Disable the cache entirely
$config->set('Cache', 'DefinitionImpl', null);
}
# Help out the Purifier a bit, until it develops this functionality
while (($cleaner = preg_replace('!<(em|strong)>(\s*)</\1>!', '$2', $input)) != $input) {
$input = $cleaner;
}
$filter = new HTMLPurifier($config);
$output = $filter->purify($input);
reference: Above code found at PHP - HTML Purifier - hello w<o>rld/world tutorial striptags
My attempt to do so which doesn't work:
class myhtmlpurifier extends HTMLPurifier
{
public function __construct()
{
require_once VS_APPLICATION_BASE.DS.'classes_3rd/htmlpurifier-4.4.0/library/HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
$config->set('Core', 'Encoding', 'UTF-8');
$config->set('HTML', 'Doctype', 'HTML 4.01 Transitional');
if (defined('PURIFIER_CACHE')) {
$config->set('Cache', 'SerializerPath', PURIFIER_CACHE);
} else {
# Disable the cache entirely
$config->set('Cache', 'DefinitionImpl', null);
}
# Help out the Purifier a bit, until it develops this functionality
while (($cleaner = preg_replace('!<(em|strong)>(\s*)</\1>!', '$2', $input)) != $input) {
$input = $cleaner;
}
$this = parent::__construct($config);
}
}
Upvotes: 0
Views: 599
Reputation: 26742
There are a two things wrong with your attempt:
$input
variable to help clean things upIn order to implement what you want, you should move the require outside, and move the while
loop into an overloaded version of the purify
method.
Upvotes: 1