将htmlpurifier扩展为个人配置

时间:2013-05-03 14:50:57

标签: php oop htmlpurifier

是否可以采用以下原始代码,并将其包装在新课程中,以便我可以创建$filter = new myPurifier();然后使用$output = $filter->purify($input);?在这篇文章的底部,您将看到我尝试失败的尝试。感谢

原始代码:

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);

参考:上面的代码位于PHP - HTML Purifier - hello w<o>rld/world tutorial striptags

我这样做的尝试不起作用:

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);
    } 
}

1 个答案:

答案 0 :(得分:1)

您的尝试有两个问题:

  • 尝试包含您从构造函数继承的类
  • 引用未定义的$input变量以帮助清理

为了实现您想要的功能,您应该将需求移到外部,并将while循环移动到purify方法的重载版本。