2012-02-03 99 views
1

所以,在我的網站上,我有用戶生成的內容。我有一個wysiwyg編輯器,但有一個視圖源代碼部分。我有幾個批准的標籤。如何阻止div內的標籤影響div外的元素?

但是,我想到,如果用戶只是放入而不關閉它呢?然後,其餘的頁面的其餘部分。 我該如何解決這個問題。

回答

0

我actaully想告訴你看看WordPress的是如何做到這一點,直到我測試了它,並發現WordPress的不關心^^我能打破我的網頁容易通過公開申報單,而不是colosing他們

反正我發現這。

/** * close all open xhtml tags at the end of the string 
* * @param string $html 
* @return string 
* @author Milian <[email protected]> 
*/function closetags($html) { 
    #put all opened tags into an array 
    preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result); 
    $openedtags = $result[1]; #put all closed tags into an array 
    preg_match_all('#</([a-z]+)>#iU', $html, $result); 
    $closedtags = $result[1]; 
    $len_opened = count($openedtags); 
    # all tags are closed 
    if (count($closedtags) == $len_opened) { 
    return $html; 
    } 
    $openedtags = array_reverse($openedtags); 
    # close tags 
    for ($i=0; $i < $len_opened; $i++) { 
    if (!in_array($openedtags[$i], $closedtags)){ 
     $html .= '</'.$openedtags[$i].'>'; 
    } else { 
     unset($closedtags[array_search($openedtags[$i], $closedtags)]); } 
    } return $html;} 

這應該是你在找什麼。

0

這是一個非常複雜的話題,並沒有簡單的捷徑。在開始重新發明輪子之前,請使用專門設計的庫。據說HTML Purifier是少數,如果不是唯一的,它是正確的。

相關問題