2012-05-09 215 views
1

我有麻烦使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选项中定义指定的元素给定的属性。

回答

1

关闭魔术引号。 (注%5C)一晚的建议

+0

我尝试过,但现在输出:一些很酷的文字 kanevbgbe

+0

看起来仍然像魔术引号对我来说... –

1

位,但我已经对HTMLPurifier剥离风格,即使他们都配置在HTML.Allowed设置属性遇到类似的问题。

我找到了解决办法,您需要同时配置CSS.AllowedProperties它看起来有点像这样:

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

使用此连同HTML.Allowed:

$config->set('HTML.Allowed', 'img[src|alt|style],span[style]'); 

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