0

是否可以采用以下原始代码,并将其包装在一个新类中,以便我可以简单地创建$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);
    } 
}
4

1 回答 1

1

您的尝试有两个问题:

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

为了实现您想要的,您应该将 require 移到外面,并将while循环移到purify方法的重载版本中。

于 2013-05-03T21:48:28.193 回答