允许在HTMLPurifier中解析完整的html

时间:2014-06-04 12:06:24

标签: php html htmlpurifier

这是我长期以来遇到的一个问题 - 我目前接受来自用户的完整html页面作为输入,并希望过滤/清除它。 HTMLpurifier的问题在于它删除了head,html和body标签 - 以及头部中的样式。我谷歌,看了论坛,尝试实现写的东西,没有运气。有人可以帮忙吗?

我想要的:保持HTML,HEAD,STYLE,BODY TAGS

我做了什么:

$config->set('HTML.DefinitionID', 'test');
    $config->set('HTML.DefinitionRev', 1);
    $config->set('HTML.AllowedElements', array('html','head', 'body', 'style', 'div', 'p'));    

    if ($def = $config->maybeGetRawHTMLDefinition()) {
        $def->addElement('html', 'Block', 'Inline', 'Common', array());
        $def->addElement('head', 'Block', 'Inline', 'Common', array());
        $def->addElement('style', 'Block', 'Inline', 'Common', array());
        $def->addElement('body', 'Block', 'Inline', 'Common', array());

    }

3 个答案:

答案 0 :(得分:0)

为什么不使用strip_tags?它支持允许的标签列表。

http://www.php.net/manual/en/function.strip-tags.php

答案 1 :(得分:0)

你需要

$config->set('Core.ConvertDocumentToFragment', false);

无论出于何种原因,Core.ConvertDocumentToFragment默认为true,即使文档指出"for most inputs, this processing is not necessary"

我也被这个咬了。我从错误收集器获得的只是隐藏的消息“删除文档元数据标签”,而这又是内部消息“Lexer:Extracted body”的翻译。

答案 2 :(得分:0)

结束结果 - HTMLPurfier本身不允许完整的HTML解析 - 扩展它或找到传递

相关问题