HTMLPurifier允许属性

时间:2012-05-09 11:24:45

标签: attributes htmlpurifier

我有麻烦使HTMLPurifier不过滤标签属性但直到现在都没有成功,我疯了。

    $config = HTMLPurifier_Config::createDefault();
    $config->set('Core.Encoding', 'UTF-8');
    $config->set('Core.CollectErrors', true);
    $config->set('HTML.TidyLevel', 'medium');
    $config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
    $config->set('URI.DisableExternalResources', false);

    $config->set('HTML.Allowed', 'table[border|width|style],tbody,tr,td,th,img[style|src|alt],span[style],p[style],ul,ol,li,strong,em,sup,sub');

    $PHTML = new HTMLPurifier($config);
    echo htmlspecialchars($PHTML->purify($html));

    // The input string:
    "Some <span style="text-decoration: underline;">cool text</span> <img src="http://someurl.com/images/logo.png" alt="" />.

    // The output string:
    "Some <span>cool text</span> <img src="%5C" alt="" />.

我想允许在HTML.Allowed选项中定义的指定元素的给定属性。

2 个答案:

答案 0 :(得分:1)

关闭魔术引号。 (注意%5C)

答案 1 :(得分:1)

有点迟到的建议,但我遇到了HTMLPurifier剥离样式属性的类似问题,即使它们是在 HTML.Allowed 设置中配置的。< / p>

我找到的解决方案要求您还配置 CSS.AllowedProperties ,它看起来有点像这样:

Search

与HTML.Allowed:

结合使用
$config->set('CSS.AllowedProperties', 'text-align,text-decoration,width,height');

我希望其他人认为这有用,you can read more about CSS.AllowedProperties here