user1032531
user1032531

Reputation: 26281

Extend htmlpurifier to personal configuration

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

Answers (1)

Edward Z. Yang
Edward Z. Yang

Reputation: 26742

There are a two things wrong with your attempt:

  • Attempting to include the class you're inheriting from from the constructor
  • Referencing an undefined $input variable to help clean things up

In 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

Related Questions