2010-03-28 24 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