2010-03-28 26 views
2

我试图使用下面的代码,它仍然去掉所有的标签。难道我做错了什么?我使用的是最新的V1.10Zend_Filter_StripTags忽略允许的标签和属性

$allowed_tags = array('img', 'object', 'param', 'embed', 'a', 'href', 'p', 'br', 'em', 'strong', 'li', 'ol', 'span'); 
$allowed_attributes = array('style', 'src', 'alt', 'href', 'width', 'height', 'value', 'name', 'type', 'embed', 'quality', 'pluginspage'); 
Zend_Loader::loadClass('Zend_Filter_StripTags'); 
$html_filter = new Zend_Filter_StripTags($allowed_tags, $allowed_attributes); 

$post = $html_filter->filter($this->_request->getPost('post')); 

因为我一直在使用相同的字符串测试的情况下,这是发生了什么事情在

<p><span style="background-color: #333399; color: #ff9900; text-decoration: underline;"><em><strong>This is a test</strong></em></span></p> 

<p><span style="background-color: #333399; color: #ff9900;"><strong><em><sub><span style="background-color: #ffffff;"><span style="color: #000000;">asdf</span></span></sub></em></strong></span></p> 

<p><span style="background-color: #333399; color: #ff9900;"><strong><em><span style="background-color: #ffffff;"><span style="color: #000000;"><sup>asdf</sup></span></span></em></strong></span></p> 

<p><span style="background-color: #333399; color: #ff9900;"><strong><em><span style="background-color: #ffffff;"><span style="color: #000000;"><img title="Cool" src="../../../public/scripts/tinymce/plugins/emotions/img/smiley-cool.gif" border="0" alt="Cool" />asdf</span></span></em></strong></span></p> 

<ul> 

<li><span style="background-color: #333399; color: #ff9900;"><strong><em><span style="background-color: #ffffff;"><span style="color: #000000;">sadf</span></span></em></strong></span></li> 

</ul> 

<ol> 

<li><span style="background-color: #333399; color: #ff9900;"><strong><em><span style="background-color: #ffffff;"><span style="color: #000000;">asdf</span></span></em></strong></span></li> 

</ol> 

这是什么出来

这是一个测试

ASDF

ASDF

ASDF

SADF

ASDF


或者,也许还有别的东西错了,因为我只是尝试这样做:

$post = strip_tags($this->_request->getPost('elm1'), '<img><object><param><embed><a><href><p><br><em><strong><li><ol><span>'); 

它剥夺了一切为好。也许有一个PHP中的设置,我错过了?

+0

测试字符串看起来很奇怪,但我测试了一个所见即所得的编辑器,所以我使用了一堆选项并抓取了html作为测试。 – Jhorra 2010-03-28 08:00:07

回答

2

按照API Doc for the StripTag Filter,构造函数签名是

void __construct ([string|array|Zend_Config $options = null]) 

因此,它应该与该作品(更新):

$html_filter = new Zend_Filter_StripTags(array(
    'allowTags' => $allowed_tags, 
    'allowAttribs' => $allowed_attributes 
)); 

在早期版本的Zend框架(1.8.4)你有要做

$html_filter = new Zend_Filter_StripTags($allowed_tags, $allowed_attributes); 

所有版本应该支持:

$html_filter = new Zend_Filter_StripTags; 
$html_filter->setAttributesAllowed($allowed_attributes); 
$html_filter->setTagsAllowed($allowed_tags); 

Internally, StripTags works with str_replace and preg_replace。所以即使有人在你的php.ini文件中将strip_tags()添加到了不允许的函数列表中,过滤器也应该可以工作。

我试过你的示例代码,它的工作。

+0

我已经尝试过你的第二种方式了,我会第一次投篮。 – Jhorra 2010-03-28 07:38:15

+0

这些都不适合我。 – Jhorra 2010-03-28 07:39:30

+0

@Jhorra你能更具体地了解当你这样做会发生什么?什么不起作用?你确定'$ this - > _ request-> getPost('post')'是一个字符串吗? – Gordon 2010-03-28 07:48:46